title: restructuring a multidimensional php array
body: <p>Currently I am trying to restructure a multidimensional array that is the result of a database query. The goal is to finally encode the array as JSON as follows, but the php function i have written isn't working (see code at end):</p>
  
  <pre><code>desiredJSONoutput = {"TypeA":[
      {"1":[
          {"TypeB":[4,5]},
          {"TypeC":[7,8,4]}]},
      {"2":[
          {"TypeB":[2,3]},
          {"TypeC":[6]}]},
      {"4":[
          {"TypeB":[33,12]},
          {"TypeC":[908]}]}]}
  </code></pre>
  
  <p>Database structure is:</p>
  
  <pre><code>fromType    fromID      toType      toID
  -----------------------------------------------------
  TypeA       1       TypeB       4
  TypeA       1       TypeB       5
  TypeA       1       TypeC       7
  TypeA       1       TypeC       8
  TypeA       1       TypeC       4
  TypeA       2       TypeB       2
  TypeA       2       TypeB       3
  TypeA       2       TypeC       6
  TypeA       2       TypeB       33
  TypeA       2       TypeB       12
  TypeA       2       TypeC       908
  </code></pre>
  
  <p>the result of my current sql query is this php array:</p>
  
  <pre><code>Array
  (
      Array
      (
          ['fromType'] =&gt; 'TypeA'
          ['fromID'] =&gt; '1'
          ['toType'] =&gt; 'TypeB'
          ['toID'] =&gt; '4'  
      )
      Array
      (
          ['fromType'] =&gt; 'TypeA'
          ['fromID'] =&gt; '1'
          ['toType'] =&gt; 'TypeB'
          ['toID'] =&gt; '5'  
      )
      Array
      (
          ['fromType'] =&gt; 'TypeA'
          ['fromID'] =&gt; '1'
          ['toType'] =&gt; 'TypeC'
          ['toID'] =&gt; '7'  
      )
      Array
      (
          ['fromType'] =&gt; 'TypeA'
          ['fromID'] =&gt; '1'
          ['toType'] =&gt; 'TypeC'
          ['toID'] =&gt; '8'  
      )
      Array
      (
          ['fromType'] =&gt; 'TypeA'
          ['fromID'] =&gt; '1'
          ['toType'] =&gt; 'TypeC'
          ['toID'] =&gt; '4'  
      )
      Array
      (
          ['fromType'] =&gt; 'TypeA'
          ['fromID'] =&gt; '2'
          ['toType'] =&gt; 'TypeB'
          ['toID'] =&gt; '2'  
      )
      Array
      (
          ['fromType'] =&gt; 'TypeA'
          ['fromID'] =&gt; '2'
          ['toType'] =&gt; 'TypeB'
          ['toID'] =&gt; '3'  
      )
      Array
      (
          ['fromType'] =&gt; 'TypeA'
          ['fromID'] =&gt; '2'
          ['toType'] =&gt; 'TypeC'
          ['toID'] =&gt; '6'  
      )
      Array
      (
          ['fromType'] =&gt; 'TypeA'
          ['fromID'] =&gt; '3'
          ['toType'] =&gt; 'TypeB'
          ['toID'] =&gt; '33'  
      )
      Array
      (
          ['fromType'] =&gt; 'TypeA'
          ['fromID'] =&gt; '3'
          ['toType'] =&gt; 'TypeB'
          ['toID'] =&gt; '12'  
      )
      Array
      (
          ['fromType'] =&gt; 'TypeA'
          ['fromID'] =&gt; '3'
          ['toType'] =&gt; 'TypeC'
          ['toID'] =&gt; '908'  
      )
  )
  </code></pre>
  
  <p>and the restructured array i need before encoding to JSON I think is:</p>
  
  <pre><code>Array
      (
          ['TypeA'] =&gt; Array
              (
                  ['1'] =&gt; Array
                      (
                          ['TypeB'] =&gt; Array
                              (
                                  [0] =&gt; 4
                                  [1] =&gt; 5
                              )
                          ['TypeC'] =&gt; Array
                              (
                                  [0] =&gt; 7
                                  [1] =&gt; 8
                                  [2] =&gt; 4
                              )
                      )
                  ['2'] =&gt; Array
                      (
                          ['TypeB'] =&gt; Array
                              (
                                  [0] =&gt; 2
                                  [1] =&gt; 3
                              )
                          ['TypeC'] =&gt; Array
                              (
                                  [0] =&gt; 6
                              )
                      )
                  ['3'] =&gt; Array
                      (
                          ['TypeB'] =&gt; Array
                              (
                                  [0] =&gt; 33
                                  [1] =&gt; 12
                              )
                          ['TypeC'] =&gt; Array
                              (
                                  [0] =&gt; 908
                              )
                      )
              )  
      )
  </code></pre>
  
  <p>I am not sure why the PHP code as follows is not doing the trick to restructure the returned php array to the structure I want:</p>
  
  <pre><code>class Utilities {
      public function linksReEncode($rowsArray) {
          $result = array();
  
          foreach ($rowsArray as $row) {
  
              $fromtype = $row['fromtype'];
              $fromID = $row['fromID'];
              $toType = $row['toType'];
              $toID = $row['toID'];
  
              $arr = $result[$fromType][$fromID][$toType];
  
              array_push($arr, $toID);
          }
      }
  }
  </code></pre>
  
  <p><strong>EDIT</strong>
  based on further research and the first answer from @Waygood here's the function that i am now using and which is working. I wonder if all the checking for existing keys is necessary and if this is the most economical way to achieve this</p>
  
  <pre><code>public function linksReEncode($rows) {
          $result = array();
  
          foreach ($rows as $row) {
  
              $fromType = $row['fromType'];
              $fromID = $row['fromID'];
              $toType = $row['toType'];
              $toID = $row['toID'];
  
              if(!array_key_exists($fromType, $result))
                  $result[$fromType] = array();
  
              if(!array_key_exists($fromID, $result[$fromType]))
                  $result[$fromType][$fromID] = array();
  
              if(!array_key_exists($toType, $result[$fromType][$fromID]))
                  $result[$fromType][$fromID][$toType] = array();
  
              array_push($result[$fromType][$fromID][$toType], $toID);
  
          }
          return $result;
      }
  </code></pre>