1function 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);
18