Skip to content

Instantly share code, notes, and snippets.

@Adamwaheed
Created February 20, 2018 10:25
Show Gist options
  • Save Adamwaheed/022fd35b19779a16aecfc46a9acd2aee to your computer and use it in GitHub Desktop.
Save Adamwaheed/022fd35b19779a16aecfc46a9acd2aee to your computer and use it in GitHub Desktop.
Tree
<?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