php - Sort array by keys numeric first than ascending by string -
i have array:
array(1,'test','jonathan')
and id end with:
array(1,'jonathan','test')
how accomplish using php?
one possible approach (demo):
$arr = array(10,'foo', 'abc', 5, 3.2, 'test','jonathan'); usort($arr, function($a, $b) { if (is_int($a) || is_float($a)) { if (is_int($b) || is_float($b)) { return $a - $b; } else return -1; } elseif (is_int($b) || is_float($b)) { return 1; } else { return strcmp($a, $b); } }); print_r($arr);
output:
array ( [0] => 3.2 [1] => 5 [2] => 10 [3] => abc [4] => foo [5] => jonathan [6] => test )
Comments
Post a Comment