重组多维 PHP 数组


restructuring a multidimensional php array

目前我正在尝试重组一个多维数组,该数组是数据库查询的结果。目标是最终将数组编码为 JSON 如下,但我编写的 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'  
    )
)

我认为在编码为 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;
    }

你只是在每个循环中重新定义$arr,而不是改变$result

改变:

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

自:

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