2012-08-07 84 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