PHP将嵌套数组转换为单个数组,同时连接父名称?

时间:2017-12-01 14:24:06

标签: php arrays

我正在导出Ebay所有类别,我想将它存储在我的数据库中,所以我会在下拉列表中显示它。

这是一个示例数组:

$foo = array(
    0 => array(
        ['category'] => array(
            ['categoryId'] => 1,
            ['categoryName'] => 'Collectables'
        ),
        ['child'] => array(
            0 => array(
                ['category'] => array(
                    ['category'] => array(
                        ['categoryId'] => 34,
                        ['categoryName'] => 'Advertising'
                    ),
                    ['child'] => array(
                        0 => array(
                            ['category'] => array(
                                ['categoryId'] => 35,
                                ['categoryName'] => 'Other Advertising'
                            ),
                        ),
                        1 => array(
                            ['category'] => array(
                                ['categoryId'] => 36,
                                ['categoryName'] => 'Drinks'
                            ),
                            ['child'] => array(
                                0 => array(
                                    ['category'] => array(
                                        ['categoryId'] => 13601,
                                        ['categoryName'] => 'Coca Cola'
                                    ),
                                )
                            )
                        )
                    )
                )
            ),
        )
    ),
    1 => array(
        ['category'] => array(
            ['categoryId'] => 99,
            ['categoryName'] => 'Everything Else'
        ),
        ['child'] => array(
            0 => array(
                ['category'] => array(
                    ['categoryId'] => 3133,
                    ['categoryName'] => 'Educational'
                ),
            ),
            1 => array(
                ['category'] => array(
                    ['categoryId'] => 14112,
                    ['categoryName'] => 'Test Auctions'
                ),
                ['child'] => array(
                    0 => array(
                        ['category'] => array(
                            ['categoryId'] => 37558,
                            ['categoryName'] => 'Attributes1'
                        ),
                    ),
                    1 => array(
                        ['category'] => array(
                            ['categoryId'] => 37561,
                            ['categoryName'] => 'Attributes4'
                        ),
                    ),
                )
            )
        )
    )
);

我希望得到这样的结果:

$result = array(
    0 => array(
        ['categoryId'] => 1,
        ['categoryName'] => 'Collectables'
    ),
    1 => array(
        ['categoryId'] => 34,
        ['categoryName'] => 'Collectables => Advertising'
    ),
    2 => array(
        ['categoryName'] => 35,
        ['categoryId'] => "Collectables => Advertising => Other Advertising",
    ),
    3 => array(
        ['categoryName'] => 36,
        ['categoryId'] => "Collectables => Advertising => Drinks ",
    ),
    4 => array(
        ['categoryName'] => 13601,
        ['categoryId'] => "Collectables => Advertising => Drinks => Coca Cola",
    ),
    5 => array(
        ['categoryName'] => 99,
        ['categoryId'] => "Everything Else",
    ),
    6 => array(
        ['categoryName'] => 3133,
        ['categoryId'] => "Everything Else => Educational",
    ),
);

等等,直到所有阵列的孩子。 我将如何编写PHP数组的递归函数来处理这个问题? 非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您的数组结构有时为[category , child ],有时为[category => [category , child ]]。如果仅保存第一种情况,则以下代码将起作用

function make($arr, $prefix='') { 
   $res = [];
   if ($prefix) $prefix .= ' => ';
   foreach($arr as $x) {
      $x['category']['categoryName'] = $prefix . $x['category']['categoryName'];
      $res[] = $x['category'];   
      if (isset($x['child'])) {
        $res = array_merge($res,  make($x['child'], $x['category']['categoryName']));
      }
   }   
   return $res;
}

make($foo);

demo