I am new to this.
I am learning ZF2 where I need to form a nested multi dimensional array from database values. My database table:
+-------------+----------------+---------------------+--------+
| 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 |
+-------------+----------------+---------------------+--------+
resultant array should be look like this:
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
)
)
)
)
)
)
I have tried this code so far but no positive result
> 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)
> );
> }
> }
> }