php order multidimensional array by key value code example
Example 1: php sort multidimensional array
function sortByAge($a, $b) {
return $a['age'] > $b['age'];
}
$people=[
["age"=>54,"first_name"=>"Bob","last_name"=>"Dillion"],
["age"=>22,"first_name"=>"Sarah","last_name"=>"Harvard"],
["age"=>31,"first_name"=>"Chuck","last_name"=>"Bartowski"]
];
usort($people, 'sortByAge');
Example 2: sort multi array php
$keys = array_column($array, 'Price');
array_multisort($keys, SORT_ASC, $array);
print_r($array);
Example 3: php sort multidimensional array by value
function sortByOrder($a, $b) {
return $a['order'] - $b['order'];
}
usort($myArray, 'sortByOrder');
Example 4: php sort multidimensional array by value
usort($myArray, function($a, $b) {
return $a['order'] <=> $b['order'];
});