Home > Backend Development > PHP Tutorial > 数组重组,该怎么处理

数组重组,该怎么处理

WBOY
Release: 2016-06-13 12:40:31
Original
920 people have browsed it

数组重组

<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 />
Copy after login

PHP 数组
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