I'am working in CodeIgniter (CI), and trying to create a nested set of category items for a dropdown list. To create a dropdown box, in CI you need to echo form_dropdown('name', $array, $selectedID)
.
Here is my function to create a nested list array:
$categoryData = array();
function list_categories($cats, $sub = ''){
foreach($cats as $cat){
//$cat['category']->id = $sub.$cat['category']->title;
$categoryData[$cat['category']->id] = $sub.$cat['category']->title;
if( sizeof($cat['children']) > 0 ){
$sub2 = str_replace('—→ ', '–', $sub);
$sub2.= '–→ ';
list_categories($cat['children'], $sub2);
}
}
}
If I will do a var_dump($categoryData);
just right after the foreach
inside the list_categories()
function, it will return the array of nested sets. So this is ok when using var_dump()
inside the function. But I need to do this:
<?php
list_categories($categories);
var_dump($categoryData);
?>
And here i get an empty array, here is an output:
array (size=0)
empty
Could someone tell me what I'am doing wrong here ?