转换php数组以选择框选项

时间:2017-12-28 12:46:34

标签: php arrays categories

我有一个带子项的输入类别数组,如下所示

我想使用递归函数将其转换为另一个数组,如

需要输出

['1'=>'时尚','10'=> '时尚>妇女”, '23'=> '时尚>女人>服装”, '29'=> '时尚>女人>服装>连衣裙......等等]

我的目的是使用输出数组填充选择框选项以添加类别。

INPUT ARRAY

阵 (     [1] =>排列         (             [id] => 1             [name] =>时尚             [parent_id] => 0             [孩子] =>排列                 (                     [10] =>排列                         (                             [id] => 10                             [name] =>妇女                             [parent_id] => 1                             [孩子] =>排列                                 (                                     [23] =>排列                                         (                                             [id] => 23                                             [name] =>服装                                             [parent_id] => 10                                             [孩子] =>排列                                                 (                                                     [29] =>排列                                                         (                                                             [id] => 29                                                             [name] =>礼服                                                             [parent_id] => 23                                                             [孩子] =>排列                                                                 (                                                                 )

                                                    )

                                                [30] => Array
                                                    (
                                                        [id] => 30
                                                        [name] => jumpsuits
                                                        [parent_id] => 23
                                                        [childs] => Array
                                                            (
                                                            )

                                                    )

                                            )

                                    )

                                [24] => Array
                                    (
                                        [id] => 24
                                        [name] => bags & accessories
                                        [parent_id] => 10
                                        [childs] => Array
                                            (
                                            )

                                    )

                                [25] => Array
                                    (
                                        [id] => 25
                                        [name] => shoes
                                        [parent_id] => 10
                                        [childs] => Array
                                            (
                                            )

                                    )

                                [26] => Array
                                    (
                                        [id] => 26
                                        [name] => watches
                                        [parent_id] => 10
                                        [childs] => Array
                                            (
                                            )

                                    )

                                [27] => Array
                                    (
                                        [id] => 27
                                        [name] => jewelery
                                        [parent_id] => 10
                                        [childs] => Array
                                            (
                                            )

                                    )

                                [28] => Array
                                    (
                                        [id] => 28
                                        [name] => eye-wear
                                        [parent_id] => 10
                                        [childs] => Array
                                            (
                                            )

                                    )

                            )

                    )

                [11] => Array
                    (
                        [id] => 11
                        [name] => men
                        [parent_id] => 1
                        [childs] => Array
                            (
                            )

                    )

                [12] => Array
                    (
                        [id] => 12
                        [name] => kids
                        [parent_id] => 1
                        [childs] => Array
                            (
                            )

                    )

                [13] => Array
                    (
                        [id] => 13
                        [name] => sports
                        [parent_id] => 1
                        [childs] => Array
                            (
                            )

                    )

                [14] => Array
                    (
                        [id] => 14
                        [name] => bags
                        [parent_id] => 1
                        [childs] => Array
                            (
                            )

                    )

                [15] => Array
                    (
                        [id] => 15
                        [name] => eyewear
                        [parent_id] => 1
                        [childs] => Array
                            (
                            )

                    )

                [16] => Array
                    (
                        [id] => 16
                        [name] => watches & jewelery
                        [parent_id] => 1
                        [childs] => Array
                            (
                            )

                    )

            )

    )

[2] => Array
    (
        [id] => 2
        [name] => supermarket
        [parent_id] => 0
        [childs] => Array
            (
                [17] => Array
                    (
                        [id] => 17
                        [name] => food & beverages
                        [parent_id] => 2
                        [childs] => Array
                            (
                                [31] => Array
                                    (
                                        [id] => 31
                                        [name] => breakfast
                                        [parent_id] => 17
                                        [childs] => Array
                                            (
                                            )

                                    )

                                [32] => Array
                                    (
                                        [id] => 32
                                        [name] => snacks
                                        [parent_id] => 17
                                        [childs] => Array
                                            (
                                            )

                                    )

                            )

                    )

                [18] => Array
                    (
                        [id] => 18
                        [name] => dairy products
                        [parent_id] => 2
                        [childs] => Array
                            (
                            )

                    )

                [19] => Array
                    (
                        [id] => 19
                        [name] => beauty
                        [parent_id] => 2
                        [childs] => Array
                            (
                            )

                    )

                [20] => Array
                    (
                        [id] => 20
                        [name] => homecare
                        [parent_id] => 2
                        [childs] => Array
                            (
                            )

                    )

                [21] => Array
                    (
                        [id] => 21
                        [name] => baby world
                        [parent_id] => 2
                        [childs] => Array
                            (
                            )

                    )

                [22] => Array
                    (
                        [id] => 22
                        [name] => pet world
                        [parent_id] => 2
                        [childs] => Array
                            (
                            )

                    )

            )

    )

[3] => Array
    (
        [id] => 3
        [name] => electronics
        [parent_id] => 0
        [childs] => Array
            (
                [33] => Array
                    (
                        [id] => 33
                        [name] => laptops
                        [parent_id] => 3
                        [childs] => Array
                            (
                            )

                    )

                [34] => Array
                    (
                        [id] => 34
                        [name] => television
                        [parent_id] => 3
                        [childs] => Array
                            (
                            )

                    )

            )

    )

[4] => Array
    (
        [id] => 4
        [name] => mobiles & tablets
        [parent_id] => 0
        [childs] => Array
            (
                [35] => Array
                    (
                        [id] => 35
                        [name] => mobiles
                        [parent_id] => 4
                        [childs] => Array
                            (
                            )

                    )

                [36] => Array
                    (
                        [id] => 36
                        [name] => tablets
                        [parent_id] => 4
                        [childs] => Array
                            (
                            )

                    )

            )

    )

[5] => Array
    (
        [id] => 5
        [name] => baby & toys
        [parent_id] => 0
        [childs] => Array
            (
            )

    )

[6] => Array
    (
        [id] => 6
        [name] => home
        [parent_id] => 0
        [childs] => Array
            (
            )

    )

[7] => Array
    (
        [id] => 7
        [name] => perfumes & beauty
        [parent_id] => 0
        [childs] => Array
            (
            )

    )

[8] => Array
    (
        [id] => 8
        [name] => sports & fitness
        [parent_id] => 0
        [childs] => Array
            (
            )

    )

[9] => Array
    (
        [id] => 9
        [name] => automotive
        [parent_id] => 0
        [childs] => Array
            (
            )

    )

我需要一个带键的输出数组,类别id

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式创建数组:

$array = array(
 'category_1' => 'value',
 'category_2' => array(
                  'subcategory_1' => 'value',
                  'subcategory_2' => 'value',
                 ),
);