2012-08-07 135 views
0

目前我正在嘗試重新構造一個數據庫查詢結果的多維數組。我們的目標是數組作爲JSON最後編碼如下,但我已經寫在PHP功能不工作(見代碼在端):重構一個多維的php數組

desiredJSONoutput = {"TypeA":[ 
    {"1":[ 
     {"TypeB":[4,5]}, 
     {"TypeC":[7,8,4]}]}, 
    {"2":[ 
     {"TypeB":[2,3]}, 
     {"TypeC":[6]}]}, 
    {"4":[ 
     {"TypeB":[33,12]}, 
     {"TypeC":[908]}]}]} 

數據庫結構是:

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 

的我目前的SQL查詢的結果是這樣的PHP數組:

Array 
(
    Array 
    (
     ['fromType'] => 'TypeA' 
     ['fromID'] => '1' 
     ['toType'] => 'TypeB' 
     ['toID'] => '4' 
    ) 
    Array 
    (
     ['fromType'] => 'TypeA' 
     ['fromID'] => '1' 
     ['toType'] => 'TypeB' 
     ['toID'] => '5' 
    ) 
    Array 
    (
     ['fromType'] => 'TypeA' 
     ['fromID'] => '1' 
     ['toType'] => 'TypeC' 
     ['toID'] => '7' 
    ) 
    Array 
    (
     ['fromType'] => 'TypeA' 
     ['fromID'] => '1' 
     ['toType'] => 'TypeC' 
     ['toID'] => '8' 
    ) 
    Array 
    (
     ['fromType'] => 'TypeA' 
     ['fromID'] => '1' 
     ['toType'] => 'TypeC' 
     ['toID'] => '4' 
    ) 
    Array 
    (
     ['fromType'] => 'TypeA' 
     ['fromID'] => '2' 
     ['toType'] => 'TypeB' 
     ['toID'] => '2' 
    ) 
    Array 
    (
     ['fromType'] => 'TypeA' 
     ['fromID'] => '2' 
     ['toType'] => 'TypeB' 
     ['toID'] => '3' 
    ) 
    Array 
    (
     ['fromType'] => 'TypeA' 
     ['fromID'] => '2' 
     ['toType'] => 'TypeC' 
     ['toID'] => '6' 
    ) 
    Array 
    (
     ['fromType'] => 'TypeA' 
     ['fromID'] => '3' 
     ['toType'] => 'TypeB' 
     ['toID'] => '33' 
    ) 
    Array 
    (
     ['fromType'] => 'TypeA' 
     ['fromID'] => '3' 
     ['toType'] => 'TypeB' 
     ['toID'] => '12' 
    ) 
    Array 
    (
     ['fromType'] => 'TypeA' 
     ['fromID'] => '3' 
     ['toType'] => 'TypeC' 
     ['toID'] => '908' 
    ) 
) 

和重組陣列I編碼爲JSON之前需要,我認爲是:

Array 
    (
     ['TypeA'] => Array 
      (
       ['1'] => Array 
        (
         ['TypeB'] => Array 
          (
           [0] => 4 
           [1] => 5 
          ) 
         ['TypeC'] => Array 
          (
           [0] => 7 
           [1] => 8 
           [2] => 4 
          ) 
        ) 
       ['2'] => Array 
        (
         ['TypeB'] => Array 
          (
           [0] => 2 
           [1] => 3 
          ) 
         ['TypeC'] => Array 
          (
           [0] => 6 
          ) 
        ) 
       ['3'] => Array 
        (
         ['TypeB'] => Array 
          (
           [0] => 33 
           [1] => 12 
          ) 
         ['TypeC'] => Array 
          (
           [0] => 908 
          ) 
        ) 
      ) 
    ) 

我不知道爲什麼PHP代碼如下沒有做的伎倆重組返回PHP數組我想要的結構:基礎上,進一步研究和第一

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); 
     } 
    } 
} 

編輯 從@Waygood回答這裏是我正在使用的功能,哪個正在工作。我不知道是否所有現有密鑰的檢查是必要的,如果這是最經濟的方式實現這一目標

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; 
    } 
+0

你額外的檢查是不是真的有必要,但檢查空或者索引的NULL值可能是。 – Waygood 2012-08-07 15:22:44

回答

1

你只是重新定義每個環內$改編,不改變$結果

變化:

$arr = $result[$fromType][$fromID][$toType]; 
array_push($arr, $toID); 

到:

$result[$fromType][$fromID][$toType][]=$toID; 
+0

我已經實現了你的建議加上一些檢查,並添加了編輯到我的原始底部顯示新的代碼。 – johowie 2012-08-07 13:13:55