我的第一个数组已按名称排序,但我想用位置对其进行排序, 我试过的是这里, 数组前的数组
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函数是:
usort($a_teams,function($a,$b){
if($a['position'] == '') return 1;
if($b['position'] == '') return -1;
return $a['position']-$b['position'];
});
结果数组是:
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] =>
)
)
但问题是当name
相同时positions
的字母顺序没有按正确顺序排列
答案 0 :(得分:0)
更改了按位置和名称排序的usort函数:
usort($a_teams,function($a,$b){
if ($a["position"]==$b["position"]){
return strcmp($a["name"], $b["name"]);
}
return ($a["position"]<$b["position"])?-1:1;
});
答案 1 :(得分:0)
更改了按位置和名称排序的usort函数:
usort($a_specials,function($a,$b){
if($a['position'] == '') return 1;
if($b['position'] == '') return -1;
if($a['position'] == $b['position'])
return strnatcmp($a['title'],$b['title']);
return $a['position']-$b['position'];
});