"平铺"多维数组"结合"儿童(即:7 x 7 = 49)

时间:2016-11-30 08:34:42

标签: php arrays algorithm recursion multidimensional-array

我已经查找了很多与此类似的线程,但主要的区别在于,在那些线程中,"孩子"元素每个只有1个值。我的问题略有不同:

你有一个2级数组,其中第一级是"选项类型" (即:颜色,尺寸,面料等),第二级是"选项值" (即:红色,蓝色,6号,7号,皮革,棉等)。我希望" flatten"这个阵列组合成一个单一级别的数组,结合了选项(即:红色 - 大小6 - 皮革,红色 - 大小6 - 棉,蓝色 - 大小6 - 皮革等)。

因此,如果有2个选项类型,每个类型有7个选项,则生成的数组将有49个键,如果有3个选项类型,每个类型有5个选项,则生成的数组将有125个键。

示例数据集(2种选项类型,每种选项有2个选项)

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

必需的数据集(2 X 2 = 4个数组键)

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 个答案:

答案 0 :(得分:0)

<强>解决方案

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