dongtaigan1594
2014-05-19 04:41
浏览 40
已采纳

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

Here is my array . I want to sort the array according the value of each key Input array:-

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
)

Output array:-

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条回答 默认 最新

  • 普通网友 2014-05-19 04:49
    最佳回答

    Indeed you should use 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);
    

    OUTPUT

     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
    )
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题