Array:
$array = [ 0=>"z01", 1=>"Z32", 2=>"z17", 3=>"Z16", ];
Need sorted results
$rs_array = [ 0=>"z01", 3=>"Z16", 2=>"z17", 1=>"Z32", ];
What’s a good way to sort?
asort($array, SORT_FLAG_CASE | SORT_NATURAL); var_dump($array);
Get:
array:4 [ 0 => "z01" 3 => "Z16" 2 => "z17" 1 => "Z32" ]
You can use user-defined comparison function, usort.
$array = [ 0=>"z01", 1=>"Z32", 2=>"z17", 3=>"Z16", ]; function cmp($a,$b){ $a = intval(substr($a, 1)); $b = intval(substr($b, 1)); if ($a == $b) { return 0; } return ($a < $b ) ? -1 : 1; } usort($array, "cmp"); print_r($array); /* Array ( [0] => z01 [1] => Z16 [2] => z17 [3] => Z32 ) */
One system function to do it
<?php $array = [ 0=>"z01", 1=>"Z32", 2=>"z17", 3=>"Z16", ]; natcasesort($array); print_r($array);
There is an array function that directly compares values
array_sort(array_values($rs_array))
Get:
You can use user-defined comparison function, usort.
One system function to do it
There is an array function that directly compares values
array_sort(array_values($rs_array))