添加到多维数组子

时间:2017-10-31 02:12:15

标签: php recursion multidimensional-array

我有这个代码列表的类别和子类别,但我需要它在childCategories里面,我已经设法在第二级添加,如果有超过第二级,我将无法添加它。场景将是这样的: -

父类别是"电子"而子类别是"智能手机"和智能手机内部的笔记本电脑一样,有华硕和三星以及同样的笔记本电脑。在笔记本电脑类别中,有" Alienware系列"和" Vostro系列"。

电子

- >智能手机

- >华硕

- >三星

- >笔记本电脑

- >惠普

- >戴尔---> Alienware系列,Vostro系列

我希望数组就像这样......

Array
        (
            [categoryId] => 3
            [title] => Electronic
            [bannerImage] => elect.jpg
            [childCategories] => Array
                (
                    Array
                        (
                            [categoryId] => 4
                            [title] => Smartphone
                            [bannerImage] => smartphone-banner.png
                            [childCategories] => Array
                                (
                                    Array
                                        (
                                        [categoryId] => 4
                                        [title] => Asus
                                        [bannerImage] => 
                                        [childCategories] => Array
                                                            (

                                                            )
                                    ),

                                    Array
                                        (
                                        [categoryId] => 5
                                        [title] => Samsung
                                        [bannerImage] => 
                                        [childCategories] => Array
                                                            (

                                                            )
                                    )


                                ),
                            Array
                            (
                                    [categoryId] => 6
                                    [title] => Laptops
                                    [bannerImage] => 
                                    [childCategories] => Array
                                    (
                                        Array
                                        (
                                        [categoryId] => 7
                                        [title] => HP
                                        [bannerImage] => 
                                        [childCategories] => Array
                                                            (

                                                            )
                                        ),

                                        Array
                                        (
                                            [categoryId] => 8
                                            [title] => Dell
                                            [bannerImage] => 
                                            [childCategories] => Array
                                                                (

                                                                    Array
                                                                    (
                                                                        [categoryId] => 9
                                                                        [title] => Alienware Series
                                                                        [bannerImage] => 
                                                                        [childCategories] => Array
                                                                        (

                                                                        )
                                                                    ),
                                                                    Array
                                                                    (
                                                                        [categoryId] => 10
                                                                        [title] => Vostro Series
                                                                        [bannerImage] => 
                                                                        [childCategories] => Array
                                                                        (

                                                                        )
                                                                    )


                                                            ),

                                        )

                                    )
                            )
                        )
                )

这是我的代码: -

if($category->getData('level') > 2) {
    $temp_array = array();

    $paths = explode("/", $category->getData('path'));
    for ($x=0; $x <= count($paths); $x++) {

        if($x == 0 || $x == 1) {
            unset($paths[$x]);
        }
        else {
            $temp_array['categoryId'] = $category->getData('entity_id');
            $temp_array['title'] = $category->getData('name');
            $temp_array['bannerImage'] = $category->getData('image');
            $temp_array['childCategories'] = array();
            $temp_array['path'] = $category->getData('path');
            $temp_array['parent'] = $category->getData('parent_id');
            $temp_array['level'] = $category->getData('level');
            $category_array[$category->getData('parent_id')]['childCategories'][$category->getData('entity_id')] = $temp_array;                     
        }
    }
}

请注意,如果将来会有更多的子类别要添加。因此它不能正常的数组赋值。

更新3

{
    "3": {
        "categoryId": "3",
        "title": "Electronic",
        "bannerImage": "elect.jpg",
        "childCategories": [],
        "parent": "2",
        "path": "1/2/3"
    },
    "4": {
        "categoryId": "4",
        "title": "Smartphone",
        "bannerImage": "smartphone-banner.png",
        "childCategories": [],
        "parent": "3",
        "path": "1/2/3/4"
    },
    "5": {
        "categoryId": "5",
        "title": "Laptop",
        "bannerImage": null,
        "childCategories": [],
        "parent": "3",
        "path": "1/2/3/5"
    },
    "6": {
        "categoryId": "6",
        "title": "Health",
        "bannerImage": null,
        "childCategories": [],
        "parent": "2",
        "path": "1/2/6"
    },
    "7": {
        "categoryId": "7",
        "title": "Vitamin",
        "bannerImage": null,
        "childCategories": [],
        "parent": "6",
        "path": "1/2/6/7"
    },
    "9": {
        "categoryId": "9",
        "title": "Fashion",
        "bannerImage": null,
        "childCategories": [],
        "parent": "2",
        "path": "1/2/9"
    },
    "10": {
        "categoryId": "10",
        "title": "For Her",
        "bannerImage": null,
        "childCategories": [],
        "parent": "9",
        "path": "1/2/9/10"
    },
    "11": {
        "categoryId": "11",
        "title": "For Him",
        "bannerImage": null,
        "childCategories": [],
        "parent": "9",
        "path": "1/2/9/11"
    },
    "12": {
        "categoryId": "12",
        "title": "Samsung",
        "bannerImage": null,
        "childCategories": [],
        "parent": "4",
        "path": "1/2/3/4/12"
    },
    "13": {
        "categoryId": "13",
        "title": "Dell",
        "bannerImage": null,
        "childCategories": [],
        "parent": "5",
        "path": "1/2/3/5/13"
    },
    "14": {
        "categoryId": "14",
        "title": "Alienware",
        "bannerImage": null,
        "childCategories": [],
        "parent": "13",
        "path": "1/2/3/5/13/14"
    },
    "15": {
        "categoryId": "15",
        "title": "Vostro",
        "bannerImage": null,
        "childCategories": [],
        "parent": "13",
        "path": "1/2/3/5/13/15"
    },
    "timestamp": "2017-10-31 04:20:20"
}

1 个答案:

答案 0 :(得分:0)

使用您的json输入数据:

$json='{
    "3": {
        "categoryId": "3",
        "title": "Electronic",
        "bannerImage": "elect.jpg",
        "childCategories": [],
        "parent": "2",
        "path": "1/2/3"
    },
    "4": {
        "categoryId": "4",
        "title": "Smartphone",
        "bannerImage": "smartphone-banner.png",
        "childCategories": [],
        "parent": "3",
        "path": "1/2/3/4"
    },
    "5": {
        "categoryId": "5",
        "title": "Laptop",
        "bannerImage": null,
        "childCategories": [],
        "parent": "3",
        "path": "1/2/3/5"
    },
    "6": {
        "categoryId": "6",
        "title": "Health",
        "bannerImage": null,
        "childCategories": [],
        "parent": "2",
        "path": "1/2/6"
    },
    "7": {
        "categoryId": "7",
        "title": "Vitamin",
        "bannerImage": null,
        "childCategories": [],
        "parent": "6",
        "path": "1/2/6/7"
    },
    "9": {
        "categoryId": "9",
        "title": "Fashion",
        "bannerImage": null,
        "childCategories": [],
        "parent": "2",
        "path": "1/2/9"
    },
    "10": {
        "categoryId": "10",
        "title": "For Her",
        "bannerImage": null,
        "childCategories": [],
        "parent": "9",
        "path": "1/2/9/10"
    },
    "11": {
        "categoryId": "11",
        "title": "For Him",
        "bannerImage": null,
        "childCategories": [],
        "parent": "9",
        "path": "1/2/9/11"
    },
    "12": {
        "categoryId": "12",
        "title": "Samsung",
        "bannerImage": null,
        "childCategories": [],
        "parent": "4",
        "path": "1/2/3/4/12"
    },
    "13": {
        "categoryId": "13",
        "title": "Dell",
        "bannerImage": null,
        "childCategories": [],
        "parent": "5",
        "path": "1/2/3/5/13"
    },
    "14": {
        "categoryId": "14",
        "title": "Alienware",
        "bannerImage": null,
        "childCategories": [],
        "parent": "13",
        "path": "1/2/3/5/13/14"
    },
    "15": {
        "categoryId": "15",
        "title": "Vostro",
        "bannerImage": null,
        "childCategories": [],
        "parent": "13",
        "path": "1/2/3/5/13/15"
    },
    "timestamp": "2017-10-31 04:20:20"
}';

我使用的是非递归方法。把孩子抱在父母身上更多的是一种堆叠技术,我先培养最深的孩子,然后从&#34;底部开始锻炼。有关逐步说明,请参阅我的内联注释。

代码:(Demo

$array=json_decode($json,true);
unset($array['timestamp']);  // remove unwanted timestamp
uasort($array,function($a,$b){return substr_count($b['path'],'/')<=>substr_count($a['path'],'/');});  // sort by depth (deepest to shallowest)
foreach($array as $key=>&$subarray){
    $data=array_intersect_key($subarray,['title'=>'','bannerImage'=>'','childCategories'=>'']);  // store only vital data based on nominated keys
    if(isset($array[$subarray['parent']])){
        $array[$subarray['parent']]['childCategories'][$key]=$data;  // insert into parent (on same level)
        unset($array[$key]);  // remove child from original "unnested" location
    }else{
        $subarray=$data;  // no movement needed, just trim unwanted elements from subarray
    }
}
ksort($array);  // resort by first level keys, ASC
var_export($array);

这是输出:

array (
  3 => 
  array (
    'title' => 'Electronic',
    'bannerImage' => 'elect.jpg',
    'childCategories' => 
    array (
      4 => 
      array (
        'title' => 'Smartphone',
        'bannerImage' => 'smartphone-banner.png',
        'childCategories' => 
        array (
          12 => 
          array (
            'title' => 'Samsung',
            'bannerImage' => NULL,
            'childCategories' => 
            array (
            ),
          ),
        ),
      ),
      5 => 
      array (
        'title' => 'Laptop',
        'bannerImage' => NULL,
        'childCategories' => 
        array (
          13 => 
          array (
            'title' => 'Dell',
            'bannerImage' => NULL,
            'childCategories' => 
            array (
              14 => 
              array (
                'title' => 'Alienware',
                'bannerImage' => NULL,
                'childCategories' => 
                array (
                ),
              ),
              15 => 
              array (
                'title' => 'Vostro',
                'bannerImage' => NULL,
                'childCategories' => 
                array (
                ),
              ),
            ),
          ),
        ),
      ),
    ),
  ),
  6 => 
  array (
    'title' => 'Health',
    'bannerImage' => NULL,
    'childCategories' => 
    array (
      7 => 
      array (
        'title' => 'Vitamin',
        'bannerImage' => NULL,
        'childCategories' => 
        array (
        ),
      ),
    ),
  ),
  9 => 
  array (
    'title' => 'Fashion',
    'bannerImage' => NULL,
    'childCategories' => 
    array (
      10 => 
      array (
        'title' => 'For Her',
        'bannerImage' => NULL,
        'childCategories' => 
        array (
        ),
      ),
      11 => 
      array (
        'title' => 'For Him',
        'bannerImage' => NULL,
        'childCategories' => 
        array (
        ),
      ),
    ),
  ),
)