php - how to usort an array which is already sorted with another key -


my first array sorted name,but want usort position, have tried here, array before usort

    array (     [0] => array         (             [name] => admin             [designation] => admin             [email] => admin@admin             [phone] => 999777788             [ext] => 67767             [position] => 1             [image] =>         )      [1] => array         (             [name] => ateam             [designation] => manager             [email] => service@mail.com             [phone] =>              [ext] => 777             [position] => 3             [image] =>          )      [2] => array         (             [name] => bteam             [designation] => manager             [email] =>g@mail.co.in             [phone] =>              [ext] =>              [position] => 4             [image] =>          )      [3] => array         (             [name] => hi team             [designation] => new             [email] => abc             [phone] => 3333             [ext] => 333             [position] => 10             [image] =>         ) [4] => array     (         [name] => new team         [designation] => maneger         [email] => mg@g         [phone] => 445567676         [ext] => ext         [position] => 10         [image] =>      ) 

)

usort function applied array is:

usort($a_teams,function($a,$b){           if($a['position'] == '') return 1;           if($b['position'] == '') return -1;           return $a['position']-$b['position'];         }); 

the result array is:

array (     [0] => array         (             [name] => admin             [designation] => admin             [email] => admin@admin             [phone] => 999777788             [ext] => 67767             [position] => 1             [image] =>          )      [1] => array         (             [name] => ateam             [designation] => manager             [email] => service@mail.com             [phone] =>              [ext] => 777             [position] => 3             [image] =>         )      [2] => array         (             [name] => bteam             [designation] => manager             [email] => g@mail.co.in             [phone] =>              [ext] =>              [position] => 4             [image] =>          )      [3] => array         (             [name] => new team             [designation] => maneger             [email] => mg@g             [phone] => 445567676             [ext] => ext             [position] => 10             [image] =>          )      [4] => array         (             [name] => hi team             [designation] => new             [email] => abc             [phone] => 3333             [ext] => 333             [position] => 10             [image] =>          )  ) 

but problem alphabetical order of name not getting in proper order when positions same

changed usort function sorting position , name:

 usort($a_teams,function($a,$b){ if ($a["position"]==$b["position"]){     return strcmp($a["name"], $b["name"]); }      return ($a["position"]<$b["position"])?-1:1; 

});


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - IE9 error '$'is not defined -