Sorting multidimensional array

半城伤御伤魂 提交于 2020-02-25 00:44:52

问题


I am adding elements to multidimensional array like this

foreach($results as $res){
  $fwidth=$res[0];
  $fpath=$res[1];

  $sub = array (
    'img_w' => $fwidth,
    'img_path' => $fpath,
  );

  $widths[] = $sub;
}

And i want to sort $widths array by 'img_w' from bigger to smaller (DESC).

Can anyone help me? Thanks in advance.


回答1:


Use PHP's USort() function (user sort), which takes two arguments. The first is the array you want to sort. The second is a function that does the actual sorting (you write it yourself). The page I linked to has some examples that should cover your case.



来源:https://stackoverflow.com/questions/5780157/sorting-multidimensional-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!