I am trying to get all the children and sub-children under each category. I have an array but I need to get this in list of category with its correspond child.
These are the given list I want.
-Category a
---category child b
-------category child c
---category d
-category b
This is an array I got return:
Array
(
[0] => Array
(
[StudentCategory] => Array
(
[id] => 1
[name] => Category A
[parent_id] => 0
[lft] => 1
[rght] => 8
[user_id] => 1
[description] => Category A
[is_active] => 1
[is_deleted] => 0
[created] => 2014-04-16 19:43:01
[updated] => 2014-04-17 02:27:28
)
[children] => Array
(
[0] => Array
(
[StudentCategory] => Array
(
[id] => 2
[name] => Category A
[parent_id] => 1
[lft] => 2
[rght] => 5
[user_id] => 1
[description] => Category A
[is_active] => 1
[is_deleted] => 0
[created] => 2014-04-16 19:44:43
[updated] => 2014-04-17 01:15:39
)
[children] => Array
(
[0] => Array
(
[StudentCategory] => Array
(
[id] => 3
[name] => Category A
[parent_id] => 2
[lft] => 3
[rght] => 4
[user_id] => 1
[description] => Category A
[is_active] => 1
[is_deleted] => 0
[created] => 2014-04-16 19:45:39
[updated] => 2014-04-16 19:45:39
)
[children] => Array
(
)
)
)
)
[1] => Array
(
[StudentCategory] => Array
(
[id] => 4
[name] => category 2
[parent_id] => 1
[lft] => 6
[rght] => 7
[user_id] => 1
[description] => category 2
[is_active] => 0
[is_deleted] => 0
[created] => 2014-04-16 20:57:28
[updated] => 2014-04-16 20:57:28
)
[children] => Array
(
)
)
)
)
[1] => Array
(
[StudentCategory] => Array
(
[id] => 5
[name] => category 21
[parent_id] => 0
[lft] => 9
[rght] => 10
[user_id] => 1
[description] => category 21
[is_active] => 1
[is_deleted] => 0
[created] => 2014-04-16 21:00:33
[updated] => 2014-04-16 21:00:33
)
[children] => Array
(
)
)
)
I am using this solution for trying to get this done:
$level = 0;
$catresult = array();
$ch = $this->Common->make_category_children_array($v['children'],$level,$catresult);
pr($ch);
function make_category_children_array($childarray,&$level,&$catresult){
foreach($childarray as $item){
$catresult[$level] = $item['StudentCategory'];
$childarray = $item['children'];
if (is_array($childarray) && $childarray) { $level++;
$this->make_category_children_array($childarray,$level,$catresult);
}
}
return $catresult;
}