Rumah >pembangunan bahagian belakang >tutorial php >ph二维数组排序难题,内附详细需求
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 />)
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'),
$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);
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 />