index.php
· 419 B · PHP
Ham
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);
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); |