1
我有一个包含像递归创建从串爆炸多维数组
Cars > Model1
Cars > Model1 > Accessories
Cars > Model1 > Something
Cars > Model2
Cars > Model3
,我要递归变换成多维阵列,其中“汽车”是型号1,模型2和的父元素很多类别字符串的数组Model3。
当然,“Accessories”和“Something”将是Model1的子元素。
一直努力到现在的工作了这背后的逻辑很长一段时间,我只是似乎无法推测出来......
编辑:我有什么,到目前为止,它的并不多。如上所述,似乎无法弄清楚如何开发这个背后的逻辑。
$array = array(
'Cars > Model1',
'Cars > Model1 > Accessories',
'Cars > Model1 > Something',
'Cars > Model2',
'Cars > Model3'
);
for ($i = 0; $i < count($array); $i++) {
$name = explode('>', $array[$i]);
// somehow check if the current "name" is already pushed to a new array - if it is, then add as a child.
}
发表您的PHP代码 –
我已经尝试了爆炸“>”和循环通过爆炸字符串将其推入$ newArray。但在每次迭代中,我需要检查这个特定的名称是否已经存在于$ newArray中 - 因为那样我们就不会添加它。 – dtn
[包含作为多维数组的字符串的路径的变量的可能重复?](http://stackoverflow.com/questions/3857033/variable-containing-a-path-as-a-string-to-multi-维数组) –