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 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献