title: Recursively traverse array and merge selected child key into parent
body: <p>I have this array where I check if the child as a particular key (<code>"hidden"</code>) is <code>true</code>. When it is, I need to append the node's <code>id</code> to the parent's key <code>"contentId"</code>.</p>
  
  <p>I have this which does the job but only for the first item in an array. I suspect the recursion is somehow broken by the <code>return</code> statement. Any ideas? Perhaps I'm missing out on a <code>array_walk_recursive</code> solution?</p>
  
  <pre><code>function bubbleUp(&amp;$tree){
      for ($i=0; $i &lt; count($tree); $i++){
          if ( isset($tree[$i]['children']) &amp;&amp; is_array($tree[$i]['children']) ) {
              $tree[$i]['contentId'] = [ $tree[$i]['id'] ];
              array_push($tree[$i]['contentId'], bubbleUp($tree[$i]['children']));
          } else {
              return reportBack($tree[$i]);
          }
      }
      return $tree;
  }
  
  function reportBack($node){
      if ( $node['hidden'] ) {
          return $node['id'];
      } else {
          return '';
      }
  }
  
  $tree = [
      [
          "name" =&gt; "Intro",
          "id" =&gt; 123,
          "hidden" =&gt; false,
          "children" =&gt; [[
              "name" =&gt; "foo",
              "id" =&gt; 452,
              "hidden" =&gt; true,
              "children" =&gt; [
                  [
                      "name" =&gt; "bar",
                      "id" =&gt; 982,
                      "hidden" =&gt; true,
                  ],
                  [
                      "name" =&gt; "gru",
                      "id" =&gt; 239,
                      "hidden" =&gt; true,
                  ]
              ]
          ]]
      ]
  ];
  bubbleUp($tree);
  echo '&lt;pre&gt;&lt;small&gt;'; print_r($tree); echo '&lt;/small&gt;&lt;/pre&gt;';
  </code></pre>
  
  <p>The end result is meant to be:</p>
  
  <pre><code>$tree = [
      [
          "name" =&gt; "Intro",
          "id" =&gt; 123,
          "hidden" =&gt; false,
          "children" =&gt; [[
              "name" =&gt; "foo",
              "id" =&gt; 452,
              "hidden" =&gt; true,
              "children" =&gt; [
                  [
                      "name" =&gt; "bar",
                      "id" =&gt; 982,
                      "hidden" =&gt; true,
                  ],
                  [
                      "name" =&gt; "gru",
                      "id" =&gt; 239,
                      "hidden" =&gt; true,
                  ]
              ],
              "contentId" =&gt; [452, 982, 239]
          ]],
          "contentId" =&gt; [123, 452, 982, 239]
      ]
  ];
  </code></pre>