建立巢狀問題:
從平面結果表中建構嵌套問題:
解:function buildTree(array $elements, $parentId = 0) { $branch = array(); foreach ($elements as $element) { if ($element['parent_id'] == $parentId) { $children = buildTree($elements, $element['id']); if ($children) { $element['children'] = $children; } $branch[] = $element; } } return $branch; } $tree = buildTree($rows);
解:
以上是遞歸函數如何從平面資料庫結果建立多維數組?的詳細內容。更多資訊請關注PHP中文網其他相關文章!