This question already asked but did not find any good answer.
To retrieve categories and subcategories in magento I am using this function:
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('name')
->addAttributeToSort('path', 'asc')
->addFieldToFilter('is_active', array('eq'=>'1'))
->load()
->toArray();
// Arrange categories in required array
$categoryList = array();
foreach ($categories as $catId => $category) {
if (isset($category['name'])) {
$categoryList[] = array(
'label' => $category['name'],
'level' =>$category['level'],
'value' => $catId
);
}
}
after that if I simply use return $categoryList;
it will not return anything(just blank) output like:
[
[
],
[
],
[
]
]
but if I use return array(array($categoryList));
in that case it will give right output.
Like this:(checking with json format)
[
[
[
{
"label": "hello",
"level": "1",
"value": 2
},
{
"label": "hello1",
"level": "2",
"value": 10
},
{
"label": "hello3",
"level": "3",
"value": 17
},
{
"label": "Tunics",
"level": "3",
"value": 18
}
]
]
]
but there are two more brackets and I do not want them and as I am new to php I do not have much Idea about how to remove them.
Is there any method which can make it right or I have to create array with different style.
try this code
Output:
if this doesn't work for you try this
var_dump($categoryList) if it was like this
then
Output: