根据php中的值对数组进行排序

时间:2014-05-19 04:41:02

标签: php arrays sorting

这是我的阵列。我想根据每个键的值对数组进行排序 输入数组: -

Array
(
    [location_classroom] => 209
    [location_daily_pe] => 1
    [location_hallways] => 3
    [location_playground] => 93
    [location_shade_area] => 26
    [location_specialist] => 8
    [location_toilet] => 3
    [location_others] => 27
    [location_others_info] => 0
)

输出数组: -

Array
(
    [location_others_info] => 0
    [location_daily_pe] => 1
    [location_hallways] => 3
    [location_toilet] => 3
    [location_specialist] => 8
    [location_shade_area] => 26
    [location_playground] => 93
    [location_classroom] => 209
    [location_others] => 27

)

1 个答案:

答案 0 :(得分:0)

确实你应该使用asort()

$arr = [
    'location_classroom' => 209,
    'location_daily_pe' => 1,
    'location_hallways' => 3,
    'location_playground' => 93,
    'location_shade_area' => 26,
    'location_specialist' => 8,
    'location_toilet' => 3,
    'location_others' => 27,
    'location_others_info' => 0
];

asort($arr);
# but you can't print the output of the sorting - it'll give you nothing meaningful (boolean)
# you should print the sorted array itself
print_r($arr);

<强>输出

 Array
(
    [location_others_info] => 0
    [location_daily_pe] => 1
    [location_toilet] => 3
    [location_hallways] => 3
    [location_specialist] => 8
    [location_shade_area] => 26
    [location_others] => 27
    [location_playground] => 93
    [location_classroom] => 209
)
相关问题