Skip to content

Instantly share code, notes, and snippets.

@gitllermopalafox
Created November 28, 2015 18:00
Show Gist options
  • Select an option

  • Save gitllermopalafox/214e33c1285325e0f5a4 to your computer and use it in GitHub Desktop.

Select an option

Save gitllermopalafox/214e33c1285325e0f5a4 to your computer and use it in GitHub Desktop.
PHP Recursive function for recursive db table
// https://stackoverflow.com/questions/8587341/recursive-function-to-generate-multidimensional-array-from-database-result/8587437#8587437
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment