Answer the question
In order to leave comments, you need to log in
How to sort a line consisting of numbers with a separator?
there is an array of strings
10/22/300
10/21/300
9/5/5/6
9/5/5/6
10/21/300
10/22/300
$element = explode('/', $item);
[9,5,5,6]
Answer the question
In order to leave comments, you need to log in
Even I didn’t understand ... so it doesn’t work?
<?php
$ar = [
'10/22/300',
'10/21/300',
'9/5/5/6'
];
natsort($ar);
print_r($ar);
/* RESULT
Array
(
[2] => 9/5/5/6
[1] => 10/21/300
[0] => 10/22/300
)
*/
If I understood the problem correctly, then you can try this:
$array = [
'10/22/300',
'10/21/300',
'9/5/5/6'
];
usort($array, function($a, $b) {
$a = array_sum(explode('/', $a));
$b = array_sum(explode('/', $b));
return ($a == $b ? 0 : ($a < $b ? -1 : 1));
});
print_r($array);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question