当前位置:Gxlcms > PHP教程 > php数组组合问题?

php数组组合问题?

时间:2021-07-01 10:21:17 帮助过:13人阅读

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

人气教程排行