dsfds4551 2016-06-30 12:30
浏览 31
已采纳

重构数组以移动键和元素

I have this array of VARIATIONS here, which contains 5 items, with their own IDs. Each item has 2 attributes, the size (Large or Small) and the color (red blue or green). I want an array that is structured like this:

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

The above code is not actual code nor does it seem like the correct syntax for an array, i just wanted to show how i would like the array to be arranged by looping through the original array.

I don't know how else to demonstrate it or explain it but i want a multidimensional array that has the FIRST attribute as the first KEY (size in this case) which has an array of the option (Large for example) which has an array of the SECOND attribute (color in this case) and in that to have an array of all the possible colors for that size, with their corresponding IDs.

Here is the full original array output that i want to manipulate and turn it into the structure described above. I've tried all kinds of array methods and functions but couldn't get the logic to work.

   [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条回答 默认 最新

  • duanbei2914 2016-06-30 13:12
    关注

    Some initialization, not really relevant, but can copy paste the code block and it works.

    $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",
                ]
            ]
        ],
    ];
    

    Do the following (notice how the array_combine will convert your attributes to a normal map).

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

    This prints:

    Array
    (
        [Large] => Array
            (
                [Blue] => 340
                [Red] => 341
            )
    
        [Small] => Array
            (
                [Blue] => 342
                [Red] => 343
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python验证码滑块图像识别
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)