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
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改