PHP: How to convert stdclass to array

Let’s see how to convert single-dimension and Multi-dimension Object to Array.

Object:

$chair = new stdClass();
$chair->type = "gaming";
$chair->price = 450;
$chair->stuff = "leather";
$chair->color = "Black & Red";

Convert Single dimension object to array.

$chairArray = (array) $chair;

Convert Multi dimensional object to array.

$chairArray = object_to_array($chair);
function objectToArray ($object) {
    if(!is_object($object) && !is_array($object)){
    	return $object;
    }
    return array_map('object_to_array', (array) $object);
}
Author: Danyal
I'm working as programmer since 2002. Born, grew up, studied and worked in Pakistan now Im located in Denmark since 2006.