Home > Backend Development > PHP Tutorial > ph二维数组排序难题,内附详细需求

ph二维数组排序难题,内附详细需求

WBOY
Release: 2016-06-13 12:22:10
Original
1049 people have browsed it

ph二维数组排序难题,请求帮助,内附详细需求
初始状态的二维数组是这样的

$a = array(<br />  array('name'=>'a','num'=>'1','parentNum'=>'0'),<br />  array('name'=>'b','num'=>'2','parentNum'=>'0'),<br />  array('name'=>'c','num'=>'3','parentNum'=>'0'),<br />  array('name'=>'d','num'=>'4','parentNum'=>'1'),<br />  array('name'=>'e','num'=>'5','parentNum'=>'2'),<br />  array('name'=>'f','num'=>'6','parentNum'=>'0'),<br />  array('name'=>'g','num'=>'7','parentNum'=>'0'),<br />)
Copy after login

默认按照name字段排序,但是现在需要改变
仍然按照name字段排序,但是同时也要按照parentNum字段排序,parentNum是代表它的父级分类,所以最终的结果应该是
  array('name'=>'a','num'=>'1','parentNum'=>'0'),<br />    array('name'=>'d','num'=>'4','parentNum'=>'1'),<br />  array('name'=>'b','num'=>'2','parentNum'=>'0'),<br />   array('name'=>'e','num'=>'5','parentNum'=>'2'),<br />  array('name'=>'c','num'=>'3','parentNum'=>'0'),<br />  array('name'=>'f','num'=>'6','parentNum'=>'0'),<br />  array('name'=>'g','num'=>'7','parentNum'=>'0'),
Copy after login

请问怎么用代码实现呢?我试了蛮久不怎么会?
------解决思路----------------------
$a = array(<br />  array('name'=>'a','num'=>'1','parentNum'=>'0'),<br />  array('name'=>'b','num'=>'2','parentNum'=>'0'),<br />  array('name'=>'c','num'=>'3','parentNum'=>'0'),<br />  array('name'=>'d','num'=>'4','parentNum'=>'1'),<br />  array('name'=>'e','num'=>'5','parentNum'=>'2'),<br />  array('name'=>'f','num'=>'6','parentNum'=>'0'),<br />  array('name'=>'g','num'=>'7','parentNum'=>'0'),<br />);

foreach($a as $r) {
$p[] = $r['parentNum'] ? $r['parentNum'] : $r['num'];
}
array_multisort($p, $a);
print_r($a);
Copy after login
Array<br />(<br />    [0] => Array<br />        (<br />            [name] => a<br />            [num] => 1<br />            [parentNum] => 0<br />        )<br /><br />    [1] => Array<br />        (<br />            [name] => d<br />            [num] => 4<br />            [parentNum] => 1<br />        )<br /><br />    [2] => Array<br />        (<br />            [name] => b<br />            [num] => 2<br />            [parentNum] => 0<br />        )<br /><br />    [3] => Array<br />        (<br />            [name] => e<br />            [num] => 5<br />            [parentNum] => 2<br />        )<br /><br />    [4] => Array<br />        (<br />            [name] => c<br />            [num] => 3<br />            [parentNum] => 0<br />        )<br /><br />    [5] => Array<br />        (<br />            [name] => f<br />            [num] => 6<br />            [parentNum] => 0<br />        )<br /><br />    [6] => Array<br />        (<br />            [name] => g<br />            [num] => 7<br />            [parentNum] => 0<br />        )<br /><br />)<br /><br />
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template