如何从简单的多维数组创建嵌套的PHP数组

时间:2016-02-01 08:59:49

标签: php arrays multidimensional-array

我有一个PHP数组如下

Array
(
    [1] => Array
        (
            [id] => 1
            [name] => Main Category
            [parent_id] => null
        )

    [2] => Array
        (
            [id] => 2
            [name] => sub category
            [parent_id] => 1
        )

    [3] => Array
        (
            [id] => 3
            [name] => child category
            [parent_id] => 2
        )

    [4] => Array
        (
            [id] => 4
            [name] => Main category 1
            [parent_id] => null
        )

    [5] => Array
        (
            [id] => 5
            [name] => sub category 1
            [parent_id] => 4
        )
    [6] => Array
        (
            [id] => 6
            [name] => child category 1
            [parent_id] => 5
        )

)

我希望使用PHP

将此数组转换如下
$categories = array(
    array(
        'id' => 1,
        'Name' => "Main Category",
        'sub' => array(
            id => 2,
            'Name' => 'Sub Category',
            'sub' => array(
                'id' => 3,
                'Name' => 'Child category',
                'sub' => 'None',
            )
        )
    ),
    array(
        'id' => 4,
        'Name' => "Main Category 1",
        'sub' => array(
            id => 5,
            'Name' => 'Sub Category 1',
            'sub' => array(
                'id' => 6,
                'Name' => 'Child category 1',
                'sub' => 'None',
            )
        )
    )
);

实际上我有一个3级列表层次结构,看起来像这样

A 1 level
 B 2 level under A
 C 2 level under A
   D level 3 under C

E 1 level
F 1 level 
  G 2 level under F

如果提供的信息不够,请在评论中向我提出任何问题。因此,我给出的示例的最终输出将如下所示

主要类别   子类别     子

主要类别1   子类别2     3类儿童

这是我尝试过的。

foreach ($categoryList as $key => $value) {
            if ($value->getCategoryId()!=1) {
                $category[$key]['id'] = $value->getCategoryId();
                $category[$key]['name'] = $value->getName();
//                $category[$key]['parent_id'] = $value->getParentCategoryIds();
                $category[$key]['sub'] = $this->createSubCategoryArray($value->getCategoryId(), $categoryList);
            }
        }


 public function createSubCategoryArray($parentCatId, $categoryList)
        {
            foreach ($categoryList as $key => $category) {
                if($category->getCategoryId() == $parentCatId && $category->getCategoryId()!=1){
                    echo "string";
                    return array(
                        'id' => $category->getCategoryId(),
                        'name' => $category->getName(),
                        'sub' => $this->createSubCategoryArray($category->getCategoryId(), $categoryList)
                    );
                }
            }
        }

1 个答案:

答案 0 :(得分:2)

您可以尝试这样的方法,并根据您的要求进行修改

for($i=0;$i<count($arr);$i++){

    if( $arr[$i]['parent_id'] == null ){
        $data[] = array(
            'id' => $arr[$i]['id'],
            'Name' => $arr[$i]['name'],
            'sub' => array()
        );
    }elseif( $arr[$i]['parent_id'] != null){
        for($j=0;$j<count($data);$j++){
            if( $data[$j]['id'] == $arr[$i]['parent_id'] ){
                $data[$j]['sub'][] = createChild($arr[$i]); 
            }else{
                for($k=0;$k<count($data[$j]['sub']);$k++){
                    if($data[$j]['sub'][$k]['id'] == $arr[$i]['parent_id']){
                        $data[$j]['sub'][$k]['sub'] = createChild($arr[$i]);    
                    }
                }
            }
        }
    }
}

function createChild($obj){
    return array(
        'id' => $obj['id'],
        'name' => $obj['name'],
        'parent_id' => $obj['parent_id'],
        'sub' => array()
    );
}