title: PHP Create a Multidimensional Array from an array with relational data
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/7767961/converting-an-array-from-one-to-multi-dimensional-based-on-parent-id-values">Converting an array from one to multi-dimensional based on parent ID values</a>  </p>
  </blockquote>
  
  
  
  <p>I am working in PHP.</p>
  
  <p>I have the following array that has relational data (parent child relationships).</p>
  
  <pre><code>Array        
  (        
      [5273] =&gt; Array        
          (        
              [id] =&gt; 5273        
              [name] =&gt; John Doe        
              [parent] =&gt;         
          )        
  
      [6032] =&gt; Array        
          (        
              [id] =&gt; 6032        
              [name] =&gt; Sally Smith        
              [parent] =&gt; 5273        
          )        
  
      [6034] =&gt; Array        
          (        
              [id] =&gt; 6034        
              [name] =&gt; Mike Jones        
              [parent] =&gt; 6032        
          )        
  
      [6035] =&gt; Array        
          (        
              [id] =&gt; 6035        
              [name] =&gt; Jason Williams        
              [parent] =&gt; 6034        
          )        
  
      [6036] =&gt; Array        
          (        
              [id] =&gt; 6036        
              [name] =&gt; Sara Johnson        
              [parent] =&gt; 5273        
          )        
  
      [6037] =&gt; Array        
          (        
              [id] =&gt; 6037        
              [name] =&gt; Dave Wilson        
              [parent] =&gt; 5273        
          )        
  
      [6038] =&gt; Array        
          (        
              [id] =&gt; 6038        
              [name] =&gt; Amy Martin        
              [parent] =&gt; 6037        
          )        
  )        
  </code></pre>
  
  <p>I need it to be in this JSON format:</p>
  
  <pre><code>{        
     "id":"5273",        
     "name":"John Doe",        
     "data":{        
  
     },        
     "children":[        
        {        
           "id":" Sally Smith",        
           "name":"6032",        
           "data":{        
  
           },        
           "children":[        
              {        
                 "id":"6034",        
                 "name":"Mike Jones",        
                 "data":{        
  
                 },        
                 "children":[        
                    {        
                       "id":"6035",        
                       "name":"Jason Williams",        
                       "data":{        
  
                       },        
                       "children":[        
                          {        
                             "id":"node46",        
                             "name":"4.6",        
                             "data":{        
  
                             },        
                             "children":[        
  
                             ]        
                          }        
                       ]        
                    }        
                 ]        
              },        
              {        
                 "id":"6036",        
                 "name":"Sara Johnson",        
                 "data":{        
  
                 },        
                 "children":[        
  
                 ]        
              },        
              {        
                 "id":"6037",        
                 "name":"Dave Wilson",        
                 "data":{        
  
                 },        
                 "children":[        
                    {        
                       "id":"6038",        
                       "name":"Amy Martin",        
                       "data":{        
  
                       },        
                       "children":[        
  
                       ]        
                    }        
                 ]        
              }        
           ]        
        }        
     ]        
  }        
  </code></pre>
  
  <p>I know I need to create a multidimensional array and run it through json_encode(). I also believe this method used to do this needs to be recursive because the real world data could have an unknown number of levels. </p>
  
  <p>I would be glad to show some of my approaches but they have not worked. </p>
  
  <p>Can anyone help me? </p>
  
  <p>I was asked to share my work. This is what I have tried but I have not gotten that close to I don't know how helpful it is.</p>
  
  <p>I made an array of just the relationships.</p>
  
  <pre><code>foreach($array as $k =&gt; $v){
      $relationships[$v['id']] = $v['parent'];
  }
  </code></pre>
  
  <p>I think (based off another SO post) used this relational data to create a the new multidimensional array. If I got this to work I was going to work on adding in the correct "children" labels etc. </p>
  
  <pre><code>$childrenTable = array();
      $data = array();
      foreach ($relationships as $n =&gt; $p) {
        //parent was not seen before, put on root
        if (!array_key_exists($p, $childrenTable)) {
            $childrenTable[$p] = array();
            $data[$p] = &amp;$childrenTable[$p];  
        }
        //child was not seen before
        if (!array_key_exists($n, $childrenTable)) {
            $childrenTable[$n] = array();
        }
        //root node has a parent after all, relocate
        if (array_key_exists($n, $data)) {
            unset($data[$n]);
        }
        $childrenTable[$p][$n] = &amp;$childrenTable[$n];      
      }
      unset($childrenTable);
  
  print_r($data);
  </code></pre>