Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert a series of parent-child relationships into a tree?
    text
    copied!<p>I am terribly stuck. I think I need to write a recursive method but I can't work out how...!</p> <p>I am trying to convert an array of parent-child relationships into a hierarchical tree which I will later display to a user.</p> <p>This is an example of the input data I might have:</p> <pre><code>$input = array( array( 'itemGroupID' =&gt; 1, 'childItemGroupID' =&gt; 2 ), array( 'itemGroupID' =&gt; 1, 'childItemGroupID' =&gt; 3 ), array( 'itemGroupID' =&gt; 1, 'childItemGroupID' =&gt; 4 ), array( 'itemGroupID' =&gt; 1, 'childItemGroupID' =&gt; 212 ), array( 'itemGroupID' =&gt; 1, 'childItemGroupID' =&gt; 339 ), array( 'itemGroupID' =&gt; 1, 'childItemGroupID' =&gt; 336 ), array( 'itemGroupID' =&gt; 1, 'childItemGroupID' =&gt; 6 ), array( 'itemGroupID' =&gt; 1, 'childItemGroupID' =&gt; 5 ), array( 'itemGroupID' =&gt; 6, 'childItemGroupID' =&gt; 8 ), array( 'itemGroupID' =&gt; 6, 'childItemGroupID' =&gt; 9 ), array( 'itemGroupID' =&gt; 6, 'childItemGroupID' =&gt; 10 ), array( 'itemGroupID' =&gt; 6, 'childItemGroupID' =&gt; 11 ), array( 'itemGroupID' =&gt; 6, 'childItemGroupID' =&gt; 12 ), array( 'itemGroupID' =&gt; 6, 'childItemGroupID' =&gt; 13 ), array( 'itemGroupID' =&gt; 6, 'childItemGroupID' =&gt; 74 ), array( 'itemGroupID' =&gt; 9, 'childItemGroupID' =&gt; 15 ), array( 'itemGroupID' =&gt; 10, 'childItemGroupID' =&gt; 16 ), array( 'itemGroupID' =&gt; 11, 'childItemGroupID' =&gt; 17 ), array( 'itemGroupID' =&gt; 12, 'childItemGroupID' =&gt; 18 ), array( 'itemGroupID' =&gt; 13, 'childItemGroupID' =&gt; 19 ), array( 'itemGroupID' =&gt; 74, 'childItemGroupID' =&gt; 75 ) ); </code></pre> <p>I wish to get back data in a format like:</p> <pre><code>$output = array( array( 'itemGroupID' =&gt; 1, 'children' =&gt; array( array( 'itemGroupID' =&gt; 2 ), array( 'itemGroupID' =&gt; 3 ), array( 'itemGroupID' =&gt; 4 ), array( 'itemGroupID' =&gt; 212 ), array( 'itemGroupID' =&gt; 339 ), array( 'itemGroupID' =&gt; 336 ), array( 'itemGroupID' =&gt; 6, 'children' =&gt; array( array( 'itemGroupID' =&gt; 8 ), array( 'itemGroupID' =&gt; 9, 'children' =&gt; array( array( 'itemGroupID' =&gt; 15 ) ) ), array( 'itemGroupID' =&gt; 10, 'children' =&gt; array( array( 'itemGroupID' =&gt; 16 ) ) ), array( 'itemGroupID' =&gt; 11, 'children' =&gt; array( array( 'itemGroupID' =&gt; 17 ) ) ), array( 'itemGroupID' =&gt; 12, 'children' =&gt; array( array( 'itemGroupID' =&gt; 18 ) ) ), array( 'itemGroupID' =&gt; 13, 'children' =&gt; array( array( 'itemGroupID' =&gt; 19 ) ) ), array( 'itemGroupID' =&gt; 74, 'children' =&gt; array( array( 'itemGroupID' =&gt; 75 ) ) ) ) ), array( 'itemGroupID' =&gt; 5 ) ) ) ); </code></pre> <p>I managed to write some code which would tell me the root(s) to start from. (As I was worried that if you were recursing from the first node it might turn out that this is from half-way down the chain already...)</p> <p>Btw, the element I output can be used to get the starting index, for each hierarchical chain.</p> <pre><code>private function _arraySearch($arr, $callback) { foreach ($arr as $key =&gt; $item) { if ($callback($item)) { return $key; } } return false; } private function _findRootsOfItemGroupTree($activeItemGroupChildren) { $searchArray = $activeItemGroupChildren; $roots = array(); foreach ($activeItemGroupChildren as $itemGroupChild) { $parentItemGroupID = $itemGroupChild['itemGroupID']; $found = array_filter($searchArray, function ($element) use ($parentItemGroupID) { return $element['childItemGroupID'] == $parentItemGroupID; }); $rootItemGroupID = $parentItemGroupID; if (count($found) == 0 &amp;&amp; $this-&gt;_arraySearch($roots, function ($element) use ($rootItemGroupID) { return $element['itemGroupID'] == $rootItemGroupID; }) === false) { $roots[] = $itemGroupChild; } } return $roots; } </code></pre> <p>However, I now need to use this information to create a new associative array. I can't work out how. (I will post some workings in a couple of minutes..)</p> <p>Ideas?</p> <p>note: assume there are no recursive loops in this structure even though they could technically exist.</p>
 

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