首页 > 后端开发 > php教程 > 【解题】PHP多维数组排序问题

【解题】PHP多维数组排序问题

WBOY
发布: 2016-06-23 14:17:42
原创
961 人浏览过

最近在碰PHP,刚好遇到一个问题,涉及到多维数组排序问题。该系列数组都有个关键key PID,要求按照PID将数组重新排列一遍。仅仅排序第一层array即可。使用了array_multisort(),但出现报错。如下:

Array(    [0] => Array        (            [pid] => 0            [title] => 首页            [path] => index            [current] => 0            [link] => /index.php        )    [1] => Array        (            [tid] => 3            [pid] => 8            [mid] => 8            [topid] => 0            [upid] => 0            [exmid] => 0            [linkid] => 2            [gotoid] => 17            [lng] => cn            [larray] => Array                (                    [0] => Array                        (                            [tid] => 17                            [pid] => 50                            [mid] => 8                            [topid] => 3                            [upid] => 3                        )                    [1] => Array                        (                            [tid] => 18                            [pid] => 50                            [mid] => 8                            [topid] => 3                            [upid] => 3                        )                    [2] => Array                        (                            [tid] => 19                            [pid] => 50                            [mid] => 8                            [topid] => 3                            [upid] => 3                        )                )        )    [2] => Array        (            [tid] => 5            [pid] => 2            [mid] => 3            [topid] => 0            [upid] => 0            [exmid] => 0            [larray] => Array                (                    [0] => Array                        (                            [tid] => 11                            [pid] => 50                            [mid] => 3                            [topid] => 5                            [upid] => 5                        )                    [1] => Array                        (                            [tid] => 12                            [pid] => 50                            [mid] => 3                            [topid] => 5                            [upid] => 5                        )                    [2] => Array                        (                            [tid] => 22                            [pid] => 50                            [mid] => 8                            [topid] => 5                            [upid] => 5                        )                )        )    [3] => Array        (            [tid] => 50            [pid] => 4            [mid] => 8            [topid] => 0            [upid] => 0            [larray] =>         )   )
登录后复制


最终结果如下:按照PID从小到大重新排列了一次。
Array(    [0] => Array        (            [pid] => 0            [title] => 首页            [path] => index            [current] => 0            [link] => /index.php        )    [1] => Array	        (            [tid] => 5            [pid] => 2            [mid] => 3            [topid] => 0            [upid] => 0            [exmid] => 0            [larray] => Array                (                    [0] => Array                        (                            [tid] => 11                            [pid] => 50                            [mid] => 3                            [topid] => 5                            [upid] => 5                        )                    [1] => Array                        (                            [tid] => 12                            [pid] => 50                            [mid] => 3                            [topid] => 5                            [upid] => 5                        )                    [2] => Array                        (                            [tid] => 22                            [pid] => 50                            [mid] => 8                            [topid] => 5                            [upid] => 5                        )                )        )    [2] => Array        (            [tid] => 50            [pid] => 4            [mid] => 8            [topid] => 0            [upid] => 0            [larray] =>         )    [3] => Array        (            [tid] => 3            [pid] => 8            [mid] => 8            [topid] => 0            [upid] => 0            [exmid] => 0            [linkid] => 2            [gotoid] => 17            [lng] => cn            [larray] => Array                (                    [0] => Array                        (                            [tid] => 17                            [pid] => 50                            [mid] => 8                            [topid] => 3                            [upid] => 3                        )                    [1] => Array                        (                            [tid] => 18                            [pid] => 50                            [mid] => 8                            [topid] => 3                            [upid] => 3                        )                    [2] => Array                        (                            [tid] => 19                            [pid] => 50                            [mid] => 8                            [topid] => 3                            [upid] => 3                        )                )        )   )
登录后复制


回复讨论(解决方案)

建议贴出 var_export();后的数组以便测试

等高手,我只会选择排序

设原数组为 $ar,则有

array_multisort(array_map(function($item){ return $item['pid']; }, $ar), $ar);
登录后复制

建议贴出 var_export();后的数组以便测试
这个就是 print_r 打印出来的

建议贴出  var_export();后的数组以便测试 

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板