首页 >后端开发 >php教程 > 数组重组,该怎么处理

数组重组,该怎么处理

WBOY
WBOY原创
2016-06-13 12:40:31928浏览

数组重组

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

PHP 数组
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn