如何从具有父 id 的数据库结果数组创建嵌套多维数组


How to created nested multi dimensional array from database result array with parent id

我是新手。

我正在学习 ZF2,我需要从数据库值形成一个嵌套的多维数组。我的数据库表:

+-------------+----------------+---------------------+--------+
| category_id | name           | parent_category_ids | status |
+-------------+----------------+---------------------+--------+
|           1 | New Category   | 1                   |      1 |
|           3 | ddd            | 1                   |      1 |
|           4 | test1          | 3                   |      0 |
|           5 | Test123 recipe | 3                   |      1 |
|           6 | abceg          | 3                   |      1 |
|           7 | xyz            | 6                   |      1 |
+-------------+----------------+---------------------+--------+

结果数组应如下所示:

Array
(
    [1] => Array
        (
            [id] => 3
            [name] => ddd
            [sub] =>
             Array
            (
                [0] => Array
                (
                    [id] => 4
                    [name] => test1
                    [sub] => None
                )
                [1] => Array
                (
                    [id] => 5
                    [name] => Test123 recipe
                    [sub] => None
                )
                [2] => Array
                (
                    [id] => 6
                    [name] => abceg
                    [sub] => 
                    Array
                    (
                        [0] => Array
                        (
                            [id] => 7
                            [name] => xyz
                            [sub] => 6
                        )
                    )
                )
            )
        )
)

到目前为止,我已经尝试了此代码,但没有积极的结果

> foreach ($categoryList as $key => $value) {
>         if ($value->getCategoryId()!=1) {
>             $category[$key]['id'] = $value->getCategoryId();
>             $category[$key]['name'] = $value->getName();
>             $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){
>                 return array(
>                     'id' => $category->getCategoryId(),
>                     'name' => $category->getName(),
>                     'sub' => $this->createSubCategoryArray($category->getCategoryId(),
> $categoryList)
>                 );
>             }
>         }
>     }

您可以反转结果并收集数组

// ordered by parent
$src = array(
    ['cid'=>'1','name'=>'Rootcat','parent'=>'1'],
    ['cid'=>'3','name'=>'dddd','parent'=>'1'],
    ['cid'=>'4','name'=>'test1','parent'=>'3'],
    ['cid'=>'5','name'=>'Test123 rec','parent'=>'3'],
    ['cid'=>'6','name'=>'abceg','parent'=>'3'],
    ['cid'=>'7','name'=>'JHGSGF','parent'=>'5'],
);
// inverse array, better take it sorted by parent_id desc
$data = array_reverse($src,false);
$for_parent = [];
$result = [];
foreach($data as $rec) {
    if (isset($for_parent[$rec['cid']])) {
        $rec['sub'] = $for_parent[$rec['cid']];
        unset($for_parent[$rec['cid']]);
    }
    $for_parent[$rec['parent']] []= $rec; 
}
print_r($for_parent);

结果示例:

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [cid] => 1
                    [name] => Rootcat
                    [parent] => 1
                    [sub] => Array
                        (
                            [0] => Array
                                (
                                    [cid] => 3
                                    [name] => dddd
                                    [parent] => 1
                                    [sub] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [cid] => 6
                                                    [name] => abceg
                                                    [parent] => 3
                                                )
                                            [1] => Array
                                                (
                                                    [cid] => 5
                                                    [name] => Test123 rec
                                                    [parent] => 3
                                                    [sub] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [cid] => 7
                                                                    [name] => JHGSGF
                                                                    [parent] => 5
                                                                )
                                                        )
                                                )
                                            [2] => Array
                                                (
                                                    [cid] => 4
                                                    [name] => test1
                                                    [parent] => 3
                                                )
                                        )
                                )
                        )
                )
        )
)