dpv50040 2014-12-05 18:23
浏览 175
已采纳

PHP按元素大小/字符串大小排序关联数组的数组

Is there a way to sort an array of associative arrays by the length of one of the elements? What I am trying to do is order the arrays by the largest to smallest description length.

$some_array = [];
$some_array[0] = ['name'=>'a name','description'=>'a description'];
$some_array[1] = ['name'=>'a name1','description'=>'a description 1'];
$some_array[2] = ['name'=>'a name2','description'=>'a description two for the third array element'];
$some_array[3] = ['name'=>'a name3','description'=>'a description three'];

With the above example $some_array[2] should come first followed by 3 then 1 then 0.

  • 写回答

2条回答 默认 最新

  • dongyun3335 2014-12-05 18:31
    关注

    PHP >= 5.5.0 needed for array_column:

    array_multisort(array_map('strlen', array_column($some_array, 'description')),
                    SORT_DESC,
                    $some_array);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?