Skip to content

Instantly share code, notes, and snippets.

@devd123
Created April 14, 2017 10:43
Show Gist options
  • Save devd123/6ab4943f784267ed06b25b08fd9e2dde to your computer and use it in GitHub Desktop.
Save devd123/6ab4943f784267ed06b25b08fd9e2dde to your computer and use it in GitHub Desktop.
Parent Child Tree PHP Recursion
public function getresultTree(array $elements, $parentId = 0) {
$branch = array();
foreach ($elements as $element) {
if ($element['parent_id'] == $parentId) {
$children = getresultTree($elements, $element['id']);
if ($children) {
$element['child'] = $children;
}
$branch[] = $element;
}
}
return $branch;
}
@Kodwings22
Copy link

When i was facing the same problem and searching the solution, i got my soloution here. You can check this link too may be useful ( https://kodlogs.net/265/php-recursive-function-to-generate-a-parent-child-tree )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment