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-維數組) –