Answers for "php usort two columns"

PHP
0

php usort two columns

$users=array(                                                       
    array("sex"=>0,"age"=>9),                              
    array("sex"=>0,"age"=>34),                             
    array("sex"=>1,"age"=>17),                             
    array("sex"=>0,"age"=>1),                              
    array("sex"=>1,"age"=>3)                              
);                                                                        
//sort first by sex, then by age
usort($users, function($a,$b){                                      
   return  ($a['sex'] == $b['sex']) ?                   
            $a['age'] < $b['age'] :                                   
            $a['sex'] < $b['sex'];                      
});
Posted by: Guest on September-23-2021

Browse Popular Code Answers by Language