Answers for "sort multidimensional array php no key"

PHP
9

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'); //$people is now sorted by age (ascending)
Posted by: Guest on August-06-2019
0

php sort multidimensional array by key

function buildSorter($key) {
    return function ($a, $b) use ($key) {
        return strnatcmp($a[$key], $b[$key]);
    };
}

usort($array, buildSorter('key_b'));
Posted by: Guest on June-25-2021

Code answers related to "sort multidimensional array php no key"

Browse Popular Code Answers by Language