Home > Backend Development > PHP Tutorial > php 数组组合有关问题

php 数组组合有关问题

WBOY
Release: 2016-06-13 12:01:38
Original
945 people have browsed it

php 数组组合问题?

//如何把下面这个这个数组<br />$arr=array(<br />0=>array('id'=>1,'pid'=>),<br />1=>array('id'=>208,'pid'=>),<br />2=>array('id'=>3,'pid'=>208)<br />3=>array('id'=>4,'pid'=>208)<br />);<br />变成这样的形式的<br />$arr=array(<br />0=>array('id'=>1,'pid'=>),<br />1=>array(<br />'id'=>208,<br />'pid'=>,<br />'str'=>array(<br />             0=>array(  'id'=>1,'pid'=>208),<br />             1=>array('id'=>4,'pid'=>208)))<br />);
Copy after login

------解决方案--------------------
http://bbs.csdn.net/topics/370094009
------解决方案--------------------
<br />$brr=array();<br />foreach($arr as $k=>$v)<br />{<br />	if( empty($v['pid']) )<br />	{<br />		if( !isset($brr[$v['id']]) )<br />			$brr[$v['id']]=$v;<br />	}else{<br />		if(!isset($brr[$v['pid']]))<br />			$brr[$v['pid']]=array();<br />		$brr[$v['pid']]['str'][]=$v;<br />	}<br />}<br />sort($brr);<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