Php array combination problem? How to put the following array $ arrarray (0 & gt; array (id & gt; 1, pid & gt;), 1 & gt; array (id & gt; 208, pid & gt;), 2 & gt; array (id & gt; 3, pid & gt; 208) 3 & gt; array (id & gt; 4, pid & gt; 2. 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 this form
$ 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 )))
);
------ Solution --------------------
Http://bbs.csdn.net/topics/370094009
------ Solution --------------------
$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);