PHP: How to convert stdclass to array

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


$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 skilled programmer with expertise in Vue.js/Nux.js for front-end development and PHP Laravel for back-end development. I excel in building APIs and services, and also have experience in web server setup & maintenance. My versatile skill set allows you to develop and maintain web applications effectively, from the user interface to the server-side functionality. I love coding with never ending learning attitude, thanks for visiting