In my magento, I have the following category tree structure like this (note: only a few categories has been expanded):

enter image description here

I would like to generate an array that looks like this:

$categories = [
    'Default Category' => [
        'HAIR' => [
            'Hair Colouring' => [
                'Permanent Hair Colour',
                // ...etc
            'Styling' => [
                'Gel / Wax / Paste / Po',
                // ...etc
        // ... etc

Is this possible? It would be a bonus if I can get the product count in each category node; just like in magento admin backend; for example: Hair colouring (1840)

I have been searching online and I found this code (my starting point):


But it doesn't quite work, this is what I get back: <ul></ul>


After doing some further searching, I had some luck loading the category tree data (linearly) like this:


require_once 'app/Mage.php';
$app = Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$categoriesArray = Mage::getModel('catalog/category')
        ->addAttributeToSort('path', 'asc')
$categories = array();
foreach ($categoriesArray as $categoryId => $category) {
    if (isset($category['name']) && isset($category['level'])) {
        $categories[] = array(
            'level'  => $category['level'],
            'label' => $category['name']


This produces the following output:

    [0] => Array
            [level] => 0
            [label] => Root Catalog
    [1] => Array
            [level] => 1
            [label] => Default Category
    [2] => Array
            [level] => 2
            [label] => HAIR
    [3] => Array
            [level] => 3
            [label] => Hair Colouring
    [4] => Array
            [level] => 4
            [label] => Permanent Hair Colour
// etc...

I am just now trying to figure out how to parse this output in the structure I want (as shown above).

  • Where you need that array frontend or backend? source data would be different depending on "category_flat" configuration. The example you'd followed doesn't covers "flat-mode" support on frontend. Jan 10, 2016 at 9:29
  • @MauroNigrele I need this data on an external php script that loads app/Mage.php to get the magento models.
    – Latheesan
    Jan 10, 2016 at 11:02
  • Mmm so the problem could be there (in the external php) unsure that app is correctly initialized as reference you can take a look at /src/shell/abstract.php you must setup some params to get all working fine. Jan 10, 2016 at 12:12
  • can you add these details and the code of your standalone php file to the question? Jan 10, 2016 at 12:16
  • @MauroNigrele I have updated my question. I found another way to get the categories, now I am trying to figure out how to parse the output into the structure I want.
    – Latheesan
    Jan 10, 2016 at 12:21

1 Answer 1


Ok, based on your Updated Question:

  • You can iterate the collection without use ->toArray() method
  • If you want to exclude the root categories add ->addAttributeToFilter('level', array('gt' => 2))

Then you can use path to sort and create the tree, you can achieve this in many ways iterating the collection and exploding the path. Ordering categories by path you will ensure create first the parents and then the childs, don't forget to use the category_id as key in the array

Alternatively you can use the built-in tree:


that retrieves a


But you can't export it to array directly, you must itarate it and get the childs recursively for each node to get a nested array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.