我有以下数组我想按值排序,A到Z,并存储在 new arary $atoz_people
中:
$people = (
[0] => Array
(
[id] => 1
[name] => Ken
)
[1] => Array
(
[id] => 2
[name] => Anne
)
[2] => Array
(
[id] => 3
[name] => Bob
)
);
我使用了" classic" asort($people)
:
$atoz_people = $people;
asort($atoz_people);
print_r($atoz_people);
不幸的是,它不起作用。为了得到下面的数组我应该改变什么?它甚至可能吗?
$atoz_people = (
[1] => Array
(
[id] => 2
[name] => Anne
)
[2] => Array
(
[id] => 3
[name] => Bob
)
[0] => Array
(
[id] => 1
[name] => Ken
)
);
答案 0 :(得分:3)
1)将初始数组存储在一个新变量中:
$atoz_people = $people;
2)创建排序功能:
function sort_by_name($a,$b)
{
return $a["name"] > $b["name"];
}
3)排序数组
uasort($atoz_people,"sort_by_name");
print_r_html($atoz_people);
@B。德赛:非常感谢!
答案 1 :(得分:0)