dongyong6045 2016-11-30 08:34
浏览 16
已采纳

通过“组合”儿童来“展平”多维数组(即:7 x 7 = 49)

I have looked up a lot threads similar to this, but the main difference is that in those threads the "children" elements only had 1 value each. My problem is slightly different:

You have a 2-level array, where the first level is "option type" (i.e: Color, Size, Fabric, etc) and the second level is the "option value" (i.e: Red, Blue, Size 6, Size 7, Leather, Cotton, etc). I would like the "flatten" this array into a single level array that combines the options (i.e: Red - Size 6 - Leather, Red - Size 6 - Cotton, Blue - Size 6 - Leather, etc).

So if there are 2 option types with 7 options each, the resulting array would have 49 keys, if there are 3 option types with 5 options each, the resulting array would have 125 keys.

Example Data Set (2 Option Types with 2 options each)

Array
(
    [0] => Array
        (
            [option_id] => 1
            [option_name] => Color
            [option_values] => Array
                (
                    [0] => Array
                        (
                            [poa_id] => 24
                            [poa_name] => Black
                            [poa] => POA575628
                            [poa_price] => 0.0000
                            [small_image] => http://img2.banggood.com/thumb/other_items//upload/2015/09/SKU279024_3.jpg
                            [view_image] => http://img3.banggood.com/thumb/view//upload/2015/09/SKU279024_3.jpg
                            [large_image] => http://img3.banggood.com/thumb/large//upload/2015/09/SKU279024_3.jpg
                            [list_grid_image] => http://img1.banggood.com/thumb/list_grid//upload/2015/09/SKU279024_3.jpg
                        )

                    [1] => Array
                        (
                            [poa_id] => 41
                            [poa_name] => Brown
                            [poa] => POA575649
                            [poa_price] => 0.0000
                            [small_image] => http://img1.banggood.com/thumb/other_items//upload/2015/09/SKU279024_5.jpg
                            [view_image] => http://img2.banggood.com/thumb/view//upload/2015/09/SKU279024_5.jpg
                            [large_image] => http://img1.banggood.com/thumb/large//upload/2015/09/SKU279024_5.jpg
                            [list_grid_image] => http://img.banggood.com/thumb/list_grid//upload/2015/09/SKU279024_5.jpg
                        )
                )
        )

    [1] => Array
        (
            [option_id] => 8
            [option_name] => Size(US)
            [option_values] => Array
                (
                    [0] => Array
                        (
                            [poa_id] => 10384
                            [poa_name] => 6.5
                            [poa] => POA575643
                            [poa_price] => 0.0000
                        )

                    [1] => Array
                        (
                            [poa_id] => 90
                            [poa_name] => 7
                            [poa] => POA575644
                            [poa_price] => 0.0000
                        )
                )
        )
)

Required Data Set (2 X 2 = 4 array keys)

Array
(
    [0] => Array
        (   
            [0] => Array
                (
                    [poa_id] => 24
                    [poa_name] => Black
                    [poa] => POA575628
                    [poa_price] => 0.0000
                    [small_image] => http://img2.banggood.com/thumb/other_items//upload/2015/09/SKU279024_3.jpg
                    [view_image] => http://img3.banggood.com/thumb/view//upload/2015/09/SKU279024_3.jpg
                    [large_image] => http://img3.banggood.com/thumb/large//upload/2015/09/SKU279024_3.jpg
                    [list_grid_image] => http://img1.banggood.com/thumb/list_grid//upload/2015/09/SKU279024_3.jpg
                )

            [1] => Array
                (
                    [poa_id] => 10384
                    [poa_name] => 6.5
                    [poa] => POA575643
                    [poa_price] => 0.0000
                )
        )

    [1] => Array
        (   
            [0] => Array
                (
                    [poa_id] => 24
                    [poa_name] => Black
                    [poa] => POA575628
                    [poa_price] => 0.0000
                    [small_image] => http://img2.banggood.com/thumb/other_items//upload/2015/09/SKU279024_3.jpg
                    [view_image] => http://img3.banggood.com/thumb/view//upload/2015/09/SKU279024_3.jpg
                    [large_image] => http://img3.banggood.com/thumb/large//upload/2015/09/SKU279024_3.jpg
                    [list_grid_image] => http://img1.banggood.com/thumb/list_grid//upload/2015/09/SKU279024_3.jpg
                )

            [1] => Array
                (
                    [poa_id] => 90
                    [poa_name] => 7
                    [poa] => POA575644
                    [poa_price] => 0.0000
                )
        )

    [2] => Array
        (   
             [0] => Array
                (
                    [poa_id] => 41
                    [poa_name] => Brown
                    [poa] => POA575649
                    [poa_price] => 0.0000
                    [small_image] => http://img1.banggood.com/thumb/other_items//upload/2015/09/SKU279024_5.jpg
                    [view_image] => http://img2.banggood.com/thumb/view//upload/2015/09/SKU279024_5.jpg
                    [large_image] => http://img1.banggood.com/thumb/large//upload/2015/09/SKU279024_5.jpg
                    [list_grid_image] => http://img.banggood.com/thumb/list_grid//upload/2015/09/SKU279024_5.jpg
                )

            [1] => Array
                (
                    [poa_id] => 10384
                    [poa_name] => 6.5
                    [poa] => POA575643
                    [poa_price] => 0.0000
                )
        )

    [3] => Array
        (   
             [0] => Array
                (
                    [poa_id] => 41
                    [poa_name] => Brown
                    [poa] => POA575649
                    [poa_price] => 0.0000
                    [small_image] => http://img1.banggood.com/thumb/other_items//upload/2015/09/SKU279024_5.jpg
                    [view_image] => http://img2.banggood.com/thumb/view//upload/2015/09/SKU279024_5.jpg
                    [large_image] => http://img1.banggood.com/thumb/large//upload/2015/09/SKU279024_5.jpg
                    [list_grid_image] => http://img.banggood.com/thumb/list_grid//upload/2015/09/SKU279024_5.jpg
                )

            [1] => Array
                (
                    [poa_id] => 90
                    [poa_name] => 7
                    [poa] => POA575644
                    [poa_price] => 0.0000
                )
        )
)
  • 写回答

1条回答 默认 最新

  • duan0065626385 2016-11-30 09:44
    关注

    Solution

    function populateOptions($options,$populatedArray)
    {
        $optionSet = array_shift($options);
        $returnArray = array();
        foreach($optionSet['option_values'] as $option)
        {
            foreach($populatedArray as $addedOption) 
            {
                $addedOption[] = $option;
                $returnArray[] = $addedOption;
            }
        }
    
        if (!empty(count($options))) $returnArray = populateOptions($options, $returnArray);
    
        return $returnArray;    
    }
    
    $productOptions = populateOptions($optionSets,array('0' => null));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?