Heim  >  Artikel  >  Backend-Entwicklung  >  php 数组组合问题?

php 数组组合问题?

WBOY
WBOYOriginal
2016-06-23 13:54:21723Durchsuche

//如何把下面这个这个数组$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);

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:成员变量问题Nächster Artikel:php ios 群体推送