Sort php multidimensional array by sub-value [duplicate]

匿名 (未验证) 提交于 2019-12-03 01:55:01

问题:

This question already has an answer here:

I have this array

Array ( [data] => Array     (         [0] => Array             (                  [id] => 1293005125                 [viewed] => TRUE                 [active] => TRUE                 [time] => December 22, 2010 13:00 hours                 [timestamp] => 1293006034                 [initial_timestamp] => 1293005125                 [user] => administrator             )          [1] => Array             (                  [mid] => 1293001908                 [viewed] => TRUE                 [active] => TRUE                 [time] => December 22, 2010 13:00 hours                 [timestamp] => 1293001908                 [initial_timestamp] => 1293001908                 [user] => administrator             )          [2] => Array             (                  [mid] => 1293009999                 [viewed] => TRUE                 [active] => TRUE                 [time] => December 22, 2010 13:00 hours                 [timestamp] => 1293009999                 [initial_timestamp] => 1293009999                 [user] => administrator             )          [3] => Array             (                  [mid] => 1293006666                 [viewed] => TRUE                 [active] => TRUE                 [time] => December 22, 2010 13:00 hours                 [timestamp] => 1293006666                 [initial_timestamp] => 1293006666                 [user] => administrator             )          [4] => Array             (                  [mid] => 1293005125                 [viewed] => TRUE                 [active] => TRUE                 [time] => December 22, 2010 13:00 hours                 [timestamp] => 1293006125                 [initial_timestamp] => 1293005125                 [user] => administrator2             )       ) 

Now I would like to sort this array by [mid] How do I do this?

Currently I sort this in a foreach loop
There has to be a better way

EDIT I hoped to output something like

[mid] key => array value

Thanks

回答1:

You can use the usort function.

function cmp($a, $b) {         return $a["mid"] - $b["mid"]; } usort($arr, "cmp"); 

See it



回答2:

The other solution is using array_multisort

 $row) {     $mid[$key]  = $row['mid']; }  // Sort the data with mid descending // Add $data as the last parameter, to sort by the common key array_multisort($mid, SORT_DESC, $data); ?> 


回答3:

Update

I recently answered this question in a much more capable manner in the "definitive" topic on sorting multidimensional arrays. The answer below targets old versions of PHP (5.2 and earlier); while the concept is sound, nowadays there are much better ways of doing things. Read the answers on other question instead.

Original (very outdated) answer

usort is there for exactly this situation. If you also need keys to be preserved, the appropriate function would be uasort.

For example:

usort($array, create_function('$a, $b',    'if ($a["mid"] == $b["mid"]) return 0; return ($a["mid"] 

Of course if you don't mind, you can declare the comparison function properly:

function compareMid($a, $b) {     if ($a['mid'] == $b['mid']) {         return 0;     }     return ($a['mid'] 

And use it like this:

usort($array, 'compareMid'); 

All of this is in the documentation.



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