Home    Articles

 

How to get all categories from magento as array list

 

In order to get all categories from magento as array, not as object use the following code.

The following code will get the categories ids from magento, will load each category, an array with data about each category will be builded and the function will return the results.

<?php
function getAllCategories()
{
	$category = Mage::getModel('catalog/category'); 
	$treeModel = $category->getTreeModel(); 
	$treeModel->load();
 
	$ids = $treeModel->getCollection()->getAllIds(); 
 
	$data = array();
 
	if (!empty($ids))
	{ 
		foreach ($ids as $id)
		{ 
			$cat = Mage::getModel('catalog/category'); 
			$cat->load($id); 
			$categoryData = array('id'=>$cat->getId(),
							  'url'=>$cat->getUrl(),
							  'name'=>$cat->getName(),
							  'image'=>$cat->getImageUrl(),
							  'isActive'=>$cat->getIsActive()
			                 );
			array_push($data, $categoryData); 
		} 
    }
 
	return $data;
}
$categories = getAllCategories();
echo '<pre/>';print_r($categories);exit;
?>

In order to get some other data about some category change the function by addind data in array.

That’s all.

by