重构数组以移动键和元素

时间:2016-06-30 12:30:15

标签: php arrays multidimensional-array

我在这里有这个VARIATIONS数组,其中包含5个项目,带有自己的ID。每个项目有2个属性,大小(大或小)和颜色(红色蓝色或绿色)。我想要一个结构如下的数组:

$array[size]=array("Large" => array("color" => array("Blue"=340,"Green"=341,"Red"=342)))
$array[size]=array("Small" => array("color" => array("Blue"=343,"Red"=345)))

上面的代码不是实际的代码,也不是一个数组的正确语法,我只是想表明我希望通过循环遍历原始数组来安排数组。

我不知道如何演示它或解释它但我想要一个多维数组,它具有FIRST属性作为第一个KEY(在这种情况下是大小),它有一个选项数组(例如Large) )具有SECOND属性的数组(在这种情况下为颜色),并且具有该大小的所有可能颜色的数组,以及它们对应的ID。

这是我想要操作并将其转换为上述结构的完整原始数组输出。我已经尝试了各种数组方法和函数,但无法使逻辑工作。

   [variations] => Array
            (
                [0] => Array
                    (
                        [id] => 340
                        [attributes] => Array
                            (
                                [0] => Array
                                    (
                                        [name] => size
                                        [option] => Large
                                    )

                                [1] => Array
                                    (
                                        [name] => color
                                        [option] => Blue
                                    )

                            )
                    )

                [1] => Array
                    (
                        [id] => 341
                        [attributes] => Array
                            (
                                [0] => Array
                                    (
                                        [name] => size
                                        [option] => Large
                                    )

                                [1] => Array
                                    (
                                        [name] => color
                                        [option] => Green
                                    )

                            )
                    )

                [2] => Array
                    (
                        [id] => 342
                        [attributes] => Array
                            (
                                [0] => Array
                                    (
                                        [name] => size
                                        [option] => Large
                                    )

                                [1] => Array
                                    (
                                        [name] => color
                                        [option] => Red
                                    )

                            )
                    )

                [3] => Array
                    (
                        [id] => 343
                        [attributes] => Array
                            (
                                [0] => Array
                                    (
                                        [name] => size
                                        [option] => Small
                                    )

                                [1] => Array
                                    (
                                        [name] => color
                                        [option] => Blue
                                    )
                            )
                    )

                [4] => Array
                    (
                        [id] => 345
                        [attributes] => Array
                            (
                                [0] => Array
                                    (
                                        [name] => size
                                        [option] => Small
                                    )

                                [1] => Array
                                    (
                                        [name] => color
                                        [option] => Red
                                    )

                            )
                    )

            )

1 个答案:

答案 0 :(得分:0)

一些初始化,并不是真正相关,但可以复制粘贴代码块并且它可以正常工作。

$arr = [];

$arr["variations"] = [
    [
        "id" => 340,
        "attributes" => [
            [
                "name" => "size",
                "option" => "Large",
            ],
            [
                "name" => "color",
                "option" => "Blue",
            ]
        ]
    ],
    [
        "id" => 341,
        "attributes" => [
            [
                "name" => "size",
                "option" => "Large",
            ],
            [
                "name" => "color",
                "option" => "Red",
            ]
        ]
    ],
    [
        "id" => 342,
        "attributes" => [
            [
                "name" => "size",
                "option" => "Small",
            ],
            [
                "name" => "color",
                "option" => "Blue",
            ]
        ]
    ],
    [
        "id" => 343,
        "attributes" => [
            [
                "name" => "size",
                "option" => "Small",
            ],
            [
                "name" => "color",
                "option" => "Red",
            ]
        ]
    ],
];

执行以下操作(注意array_combine如何将属性转换为法线贴图)。

$array = [];

foreach ($arr["variations"] as $entry) {
    $attributes = array_combine(array_column($entry["attributes"], "name"), array_column($entry["attributes"], "option"));
    if (!isset($array[$attributes["size"]])) {
        $array[$attributes["size"]] = [];
    }
    $array[$attributes["size"]][$attributes["color"]] = $entry["id"];
}


print_r($array);

打印:

Array
(
    [Large] => Array
        (
            [Blue] => 340
            [Red] => 341
        )

    [Small] => Array
        (
            [Blue] => 342
            [Red] => 343
        )

)