时间:2021-07-01 10:21:17 帮助过:2人阅读
[{id=>'00'},
{id=>'01'},
{id=>'22'},
{id='0001'},
{id=>'2234'}]
[
{id=>'00',children=>{{id=>'0001'}} },
{id=>'01',children=>{} },
{id=>'22',children=>{{id=>'2234'}} }
]
$a = array('4401','00','0001','0002','22','2201');
$b = array();
for($i = 0; $i < count($a); $i++)
{
if(strlen($a[$i]) == 2)
{
if(!array_key_exists($a[$i],$b))
{
$b[$a[$i]] = array();
}
}
else
{
$id = substr($a[$i], 0, 2);
if(isset($b[$id]))
{
if(!in_array($a[$i], $b[$id]))
{
$b[$id][] = $a[$i];
}
}
else
{
$b[$id] = array();
$b[$id][] = $a[$i];
}
}
}
print_r($b);