stuzer05 revised this gist 2 years ago. Go to revision
1 file changed, 17 insertions
index.php(file created)
| @@ -0,0 +1,17 @@ | |||
| 1 | + | function buildTree(array $elements, $parentId = 0) { | |
| 2 | + | $branch = array(); | |
| 3 | + | ||
| 4 | + | foreach ($elements as $element) { | |
| 5 | + | if ($element['parent_id'] == $parentId) { | |
| 6 | + | $children = buildTree($elements, $element['id']); | |
| 7 | + | if ($children) { | |
| 8 | + | $element['children'] = $children; | |
| 9 | + | } | |
| 10 | + | $branch[] = $element; | |
| 11 | + | } | |
| 12 | + | } | |
| 13 | + | ||
| 14 | + | return $branch; | |
| 15 | + | } | |
| 16 | + | ||
| 17 | + | $tree = buildTree($rows); | |
Newer
Older