0
如何通過遞歸將一個數組轉換爲其他數組?這個例子僅適用於第二級。如何做遞歸?
$array2 = array();
foreach ($array as $levelKey => $level) {
foreach ($level as $itemKey => $item) {
if (isset($array[$levelKey + 1])) {
$array2[$item['data']['id']] = $item;
$children = $this->searchChildren($item['data']['id'], $array[$levelKey + 1]);
$array += $children;
}
}
}
function searchChildren($parent_id, $level)
{
$_children = array();
foreach ($level as $key => $item) {
if ($item['data']['parent_id'] === $parent_id) {
$_children[$key] = $item;
}
}
return $_children;
}
你必須自己調用你的函數...... – crush 2013-03-07 14:28:56
爲了理解遞歸,你必須瞭解遞歸。 – ddinchev 2013-03-07 14:29:08
當你在我的例子中顯示它? – tomasr 2013-03-07 14:30:31