Created
February 20, 2018 10:25
-
-
Save Adamwaheed/022fd35b19779a16aecfc46a9acd2aee to your computer and use it in GitHub Desktop.
Tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Model\Common; | |
class Tree { | |
//building tree | |
public function buildTree(array $elements, $parentId = 0) { | |
$branch = array(); | |
$child = array(); | |
foreach ($elements as $element) { | |
if ($element['parent_id'] == $parentId) { | |
$children = $this->buildTree($elements, $element['id']); | |
if ($children) { | |
$element['items'] = $children; | |
} | |
$child = array_add($element, 'items', array()); | |
$branch[] = $child; | |
} | |
} | |
return $branch; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment