日期:2014-05-17  浏览次数:20576 次

数组重组

#原数组
$list = array(
0=>array('mid'=>1,'menu'=>'m1','level'=>1,'belong'=>0,'higher'=>'a1'),
1=>array('mid'=>1,'menu'=>'m1','level'=>1,'belong'=>0,'higher'=>'a2'),
2=>array('mid'=>1,'menu'=>'m1','level'=>1,'belong'=>0,'higher'=>'a3'),
3=>array('mid'=>2,'menu'=>'m2','level'=>1,'belong'=>0,'higher'=>' '),
4=>array('mid'=>3,'menu'=>'m3','level'=>1,'belong'=>0,'higher'=>' '),
5=>array('mid'=>4,'menu'=>'m4','level'=>1,'belong'=>0,'higher'=>'d1'),
6=>array('mid'=>5,'menu'=>'m5','level'=>1,'belong'=>0,'higher'=>'e1'),
7=>array('mid'=>5,'menu'=>'m5','level'=>1,'belong'=>0,'higher'=>'e2')
);
#从原数组中提取新数组
$menuArr = array(
0=>array('mid'=>1,'main'=>'m1','child'=>array('a1','a2','a3') ),
1=>array('mid'=>2,'main'=>'m2','child'=>' ',
2=>array('mid'=>3,'main'=>'m3','child'=>' ',
3=>array('mid'=>4,'main'=>'m4','child'=>array('d1') ),
4=>array('mid'=>5,'main'=>'m5','child'=>array('e1','e2') ),
);
#自己搞半天搞晕了:
$sign = null;
$n = 0;
$menuArr = array();
foreach($list as $men){
if($sign == $men['mid']){
echo "n:{$n}<br>";
$menuArr[$men['mid']]['main'] = $men['menu'];
$menuArr[$men['mid']]['child'][$n] = $men['higher'];  #提示这里不可解析
//array_push($menuArr[$men['mid']]['child'], $men['higher']);
$n++;
}else{
$n = 0;
$menuArr[$men['mid']]['main'] = $men['menu'];
$menuArr[$men['mid']]['child'] = $men['higher'];
}
$sign = $men['mid'];
}
print_r($menuArr);
#帮帮忙……
PHP 数组

------解决方案--------------------
$list = array(
    0=>array('mid'=>1,'menu'=>'m1','level'=>1,'belong'=>0,'higher'=>'a1'),
    1=>array('mid'=>1,'menu'=>'m1','level'=>1,'belong'=>0,'higher'=>'a2'),
    2=>array('mid'=>1,'menu'=>'m1','level'=>1,'belong'=>0,'higher'=>'a3'),