convert object in to array using php

how to convert object in to array using php?

As you seen in my previous post that how to fetch products of particular category in magento, but its return object instead of array. when we work with the open source then most of open source return object instead of array. if you have not much more idea on the object or you do not know oops then don’t worry, i have solution for you which is very easy and using that you can convert your object in to array, then you can process that array. below is the code which is used to convert the object in to array.

 
<?php
 
function ConvertObjectToArray($obj)
{
foreach($obj as $tempobject)
{
if( !is_object( $tempobject ) && !is_array( $tempobject ) )
{
return $tempobject;
}
if( is_object( $tempobject ) )
{
$tempobject = get_object_vars( $tempobject );
}
$newarray[] = $tempobject;
}
return $newarray;
}
 
?>

you need to call the function named ConvertObjectToArray and pass your object as a parameter, and this function will convert object in to array and return array.

Its Simple but if you get any issue then you can contact me.

fetch products of particular category in magento

how to fetch category product saperately?

Recently, i am working with the magento project and i need to display first 5 products of specific category, if you look in some site then they display running products at the widget, or at left or right side. same way i need to display 5 products of particular category in magento site. after searching i got the solution and its really very very easy to fetch product of specific category in magento. below is the code which is used to fetch the products of particular category.

 
$catObj = Mage::getModel('catalog/category')->load($catId);
$proObj = $catObj->getProductCollection()->getData();

Here, $catId is the category id whose product you need to fetch, once this statements execute then you get all the product in the $proObj Object, please keep in mind that its return result in object not in array.

I hope it will be useful in your project, so enjoy…..