Note that there are some explanatory texts on larger screens.

plurals
  1. POBuild a tree from a flat array in PHP
    primarykey
    data
    text
    <p>I've looked around the internet and haven't quite found what I'm looking for. I have a flat array with each element containing an 'id' and a 'parent_id'. Each element will only have ONE parent, but may have multiple children. If the parent_id = 0, it is considered a root level item. I'm trying to get my flat array into a tree. The other samples I have found only only copy the element to the parent, but the original still exists.</p> <p><strong>EDIT</strong></p> <p>Each element of the starting array is read from a separate XML file. The file itself will have '0' as the value for parent_id if it doesn't have a parent. The keys are actually strings.</p> <p>I'm sorry for the confusion earlier. Hopefully this is more clear:</p> <p><strong>/EDIT</strong></p> <p>My starting array:</p> <pre> Array ( [_319_] => Array ( [id] => 0 [parent_id] => 0 ) [_320_] => Array ( [id] => _320_ [parent_id] => 0 ) [_321_] => Array ( [id] => _321_ [parent_id] => _320_ ) [_322_] => Array ( [id] => _322_ [parent_id] => _321_ ) [_323_] => Array ( [id] => _323_ [parent_id] => 0 ) [_324_] => Array ( [id] => _324_ [parent_id] => _323_ ) [_325_] => Array ( [id] => _325_ [parent_id] => _320_ ) )</pre> <p>The resulting array after the tree is made:</p> <pre> Array ( [_319_] => Array ( [id] => _319_ [parent_id] => 0 ) [_320_] => Array ( [id] => _320_ [parent_id] => 0 [children] => Array ( [_321_] => Array ( [id] => _321_ [parent_id] => _320_ [children] => Array ( [_322_] => Array ( [id] => _322_ [parent_id] => _321_ ) ) ) [_325_] => Array ( [id] => _325_ [parent_id] => _320_ ) ) [_323_] => Array ( [id] => _323_ [parent_id] => 0 [children] => Array ( [_324_] => Array ( [id] => _324_ [parent_id] => _323_ ) ) ) </pre> <p>Any help / guidance is greatly appreciated!</p> <p>Some code I have so far:</p> <pre> function buildTree(array &$elements, $parentId = 0) { $branch = array(); foreach ($elements as $element) { if ($element['parent_id'] == $parentId) { $children = $this->buildTree($elements, $element['id']); if ($children) { $element['children'] = $children; } $branch[] = $element; } } return $branch; } </pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload