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

php 数组组合问题?

WBOY
WBOYOriginal
2016-06-23 13:54:21764browse

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


回复讨论(解决方案)

http://bbs.csdn.net/topics/370094009

$brr=array();foreach($arr as $k=>$v){	if( empty($v['pid']) )		if( isset($brr[$v['id']]) )		{			$brr[$v['id']][]=$v['id'];			$brr[$v['id']][]=$v['pid'];		}else			$brr[$v['id']]=$v;	else{		if(!isset($brr[$v['pid']]))			$brr[$v['pid']]=array();		$brr[$v['pid']]['str'][]=$v;	}}sort($brr);

http://bbs.csdn.net/topics/370094009



版主这个得到的结果是这样的,如果把建为4的去掉就好了啊,因为建为4的已经放到208里面了,就不需要了
Array(    [1] => Array        (            [id] => 1            [pid] =>         )    [4] => Array        (            [id] => 4            [pid] => 208        )    [208] => Array        (            [id] => 208            [pid] =>             [child] => Array                (                    [4] => Array                        (                            [id] => 4                            [pid] => 208                        )                )        ))

$brr=array();foreach($arr as $k=>$v){	if( empty($v['pid']) )	{		if( !isset($brr[$v['id']]) )			$brr[$v['id']]=$v;	}else{		if(!isset($brr[$v['pid']]))			$brr[$v['pid']]=array();		$brr[$v['pid']]['str'][]=$v;	}}sort($brr);

Statement:
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
Previous article:成员变量问题Next article:php ios 群体推送