Php array combination problem?
// How to put the following array $ arr = array (0 => array ('id' => 1, 'pid' => ), 1 => array ('id' => 208, 'pid '=>), 2 => array ('id' => 3, 'pid' => 208) 3 => array ('id' => 4, 'pid' => 208 )); in the form of $ 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 ))));
Reply to discussion (solution)
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
The result obtained by the moderator is as follows. if you just remove the value created as 4, because the value created as 4 has already been placed in 208, you don't need it anymore.
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);