Answers for "php sort json array"

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

sort json in php

<?php                                                                                                                                                                                                       
usort($data, function($a, $b) { //Sort the array using a user defined function
    return $a->score > $b->score ? -1 : 1; //Compare the scores
});                                                                                                                                                                                                        

print_r($data);   
?>
Posted by: Guest on August-05-2020

Browse Popular Code Answers by Language