title: create multidimensional array from json object list
body: <p>I have a json feed that is more or less a list of objects
  each has it's own id and idParent. objects that have idParent of null are the base parent elements. What I'm trying to achieve is to make a proper multidimensional array like a tree view. Keep in mind that children can have children too.</p>
  
  <pre><code>{
      "obj1":{
          "idParent":null,
          "id":"parent1"
      },
      "obj2":{
          "idParent":null,
          "id":"parent2"
      },
      "obj3":{
          "idParent":null,
          "id":"parent3"
      },
      "obj4":{
          "idParent":null,
          "id":"parent4"
      },
      "obj5":{
          "idParent":null,
          "id":"parent5"
      },                      
      "obj6":{
          "idParent":"parent1",
          "id":"layer1-1"
      },
      "obj7":{
          "idParent":"parent1",
          "id":"layer1-2"
      },
      "obj8":{
          "idParent":"parent2",
          "id":"layer1-3"
      },
      "obj9":{
          "idParent":"parent4",
          "id":"layer1-4"
      },
      "obj10":{
          "idParent":"parent3",
          "id":"layer1-5"
      },                      
      "obj11":{
          "idParent":"layer1-1",
          "id":"layer2-1"
      },
      "obj12":{
          "idParent":"parent5",
          "id":"layer2-2"
      },
      "obj13":{
          "idParent":"layer1-4",
          "id":"layer2-3"
      },
      "obj14":{
          "idParent":"layer1-5",
          "id":"layer2-4"
      },
      "obj15":{
          "idParent":"layer1-5",
          "id":"layer2-5"
      }       
  }
  </code></pre>
  
  <p>I've managed to filter out the root parents but after that I fail very bad
  The first function does filter out the root parent nodes with idParent of null.</p>
  
  <pre><code>function decodeData($data) {
  
      global $out;
  
      foreach ($data as $key =&gt; $obj) {
          if (is_array($obj)) {
              foreach ($obj as $prop =&gt; $value) {
                  if ($prop == 'idParent') {
                      if($value == null) {
                          array_push($out, $obj);
                          unset($data[$key]);
                      }   
                  }
              }
          }
      }
  
      if (count($data) &gt; 0) {
          decodeData($data);
      } else {
          echo json_encode(array('length'=&gt;count($data)));
      }
  
  
  }
  </code></pre>
  
  <p>And this is what I'm experimenting on with no result</p>
  
  <pre><code>function decodeData($arrays) {
  
      global $out;
  
      foreach ($arrays as $array_name =&gt; $arr) {
          foreach ($arr as $arr_prop =&gt; $arr_val) {
              if ($arr_prop == 'idParent' &amp;&amp; $arr_val ==  null) { // we found root parents
                  array_push($out, $arr);
                  unset($arrays[$array_name]);    //remove array from the list
              } else { // check if idParent is inside out
                  foreach ($out as $out_arr_name =&gt; $out_arr) { // iterate through out arrays
                      foreach ($out_arr as $out_arr_prop =&gt; $out_prop_val) { //
                          if ($out_arr_prop == 'id' &amp;&amp; $arr_prop == 'idParent' &amp;&amp; $out_arr_val == $arr_val) {
                              array_push($out_arr['children'], $obj);
                              unset($arrays[$array_name]);
                          }
                      }
                  }
              }
  
          }
      }
  
  
      if (count($arrays) &gt; 0) {
          decodeData($arrays);
      } else {
          echo json_encode(array('length'=&gt;count($arrays)));
      }
  
  
  }
  </code></pre>
  
  <p>If anyone could provide some help I would really appreciate it.</p>