I know this is old but you could try the following piece of code:

$array = json_decode(json_encode($object), true);

where $object is the response of the API.

Answer from kendepelchin on Stack Overflow
🌐
GitHub
gist.github.com › esilvajr › 26beaf1422922df320a4e108047862aa
This is a basic solution when we need to convert a array of objects into a multidimensional array in PHP. · GitHub
May 23, 2020 - Então, iteramos as propriedades do objeto e configuramos cada propriedade para acessivel convertendo assim cada propriedado do objeto para um membro array. Veja um exemplo completo: https://gist.github.com/esilvajr/26beaf1422922df320a4e108047862aa#file-convertarrayofobjectsample-php
🌐
GitHub
gist.github.com › SeanCannon › 6585889
PHP array_flatten() function. Convert a multi-dimensional array into a single-dimensional array. · GitHub
PHP array_flatten() function. Convert a multi-dimensional array into a single-dimensional array. - array_flatten.php
🌐
GitHub
gist.github.com › piercemoore › 3107841
Convert a multi-dimensional array into an object · GitHub
Convert a multi-dimensional array into an object · Raw · arrayToObject.php · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
GitHub
gist.github.com › dmitrymomot › 3920125
Convert a multi-dimensional array into a stdClass object. Pass by reference. · GitHub
Convert a multi-dimensional array into a stdClass object. Pass by reference. - array_to_object.php
Find elsewhere
Top answer
1 of 2
126

A quick way to do this is:

$obj = json_decode(json_encode($array));

Explanation

json_encode($array) will convert the entire multi-dimensional array to a JSON string. (php.net/json_encode)

json_decode($string) will convert the JSON string to a stdClass object. If you pass in TRUE as a second argument to json_decode, you'll get an associative array back. (php.net/json_decode)

I don't think the performance here vs recursively going through the array and converting everything is very noticeable, although I'd like to see some benchmarks of this. It works, and it's not going to go away.

2 of 2
7

The best way would be to manage your data structure as an object from the start if you have the ability:

$a = (object) array( ... ); $a->prop = $value; //and so on

But the quickest way would be the approach supplied by @CharlieS, using json_decode(json_encode($a)).

You could also run the array through a recursive function to accomplish the same. I have not benchmarked this against the json approach but:

function convert_array_to_obj_recursive($a) {
    if (is_array($a) ) {
        foreach($a as $k => $v) {
            if (is_integer($k)) {
                // only need this if you want to keep the array indexes separate
                // from the object notation: eg. $o->{1}
                $a['index'][$k] = convert_array_to_obj_recursive($v);
            }
            else {
                $a[$k] = convert_array_to_obj_recursive($v);
            }
        }

        return (object) $a;
    }

    // else maintain the type of $a
    return $a; 
}

Hope that helps.

EDIT: json_encode + json_decode will create an object as desired. But, if the array was numerical or mixed indexes (eg. array('a', 'b', 'foo'=>'bar') ), you will not be able to reference the numerical indexes with object notation (eg. $o->1 or $o[1]). the above function places all the numerical indexes into the 'index' property, which is itself a numerical array. so, you would then be able to do $o->index[1]. This keeps the distinction of a converted array from a created object and leaves the option to merge objects that may have numerical properties.

🌐
Fellow Tuts
fellowtuts.com › home › php › 3 ways – multidimensional array to object conversion
3 Ways - Multidimensional Array to Object Conversion
December 18, 2018 - But if any only if the array is not multidimensional. In other words, it’s not a nested array (array in the array). If you did so then only the top level keys => values of the array will be converted to object. A combination of PHP’s JSON encode and decode functions can magically do the conversion.
🌐
GitHub
gist.github.com › victorbstan › 744478
recursively cast a PHP object to array · GitHub
See here https://www.php.net/manual/de/function.json-encode.php#115733 ... This will not be be applicable if the properties of the object you are trying to convert are declared as private
🌐
IQCode
iqcode.com › code › php › php-convert-multidimensional-object-to-array
php convert multidimensional object to array Code Example
September 29, 2021 - $person = new stdClass(); $person->firstName = "Taylor"; $person->age = 32; //Convert Single-Dimention Object to array $per...
🌐
Position Is Everything
positioniseverything.net › home › php object to array: a comprehensive guide with examples
PHP Object to Array Conversion: A Complete and Easy-to-Follow Guide
December 29, 2025 - In this example of converting an associative array into an object, the array is converted to object using, $object = (object) $array. Lastly, we print out the output using the function var_dump(variable of an object is written here).
🌐
W3Docs
w3docs.com › php
Convert multidimensional array into single array
To convert a multidimensional array into a single array in PHP, you can use the array_merge() function.
🌐
onlinecode
onlinecode.org › home › php convert multidimensional array to object – programming
PHP Convert Multidimensional Array To Object – Programming
March 16, 2023 - Today, We want to share with you PHP Convert Multidimensional Array To Object.In this post we will show you PHP – Convert mixed array & objects recursively, hear for Multidimensional Array to Object Conversion we will give you demo and example for implement.In this post, we will learn about Map and object: converting nested objects using PHP with an example.
🌐
Coderwall
coderwall.com › p › 8mmicq › php-convert-mixed-array-objects-recursively
PHP - Convert mixed array & objects recursively (Example)
June 11, 2021 - <?php function object_to_array($d) { if (is_object($d)) $d = get_object_vars($d); return is_array($d) ? array_map(__FUNCTION__, $d) : $d; } function array_to_object($d) { return is_array($d) ? (object) array_map(__FUNCTION__, $d) : $d; } ?> ... Whoops. I should have probably included that this applies to mixed multidimensional arrays and objects.
🌐
Bobbyallen
blog.bobbyallen.me › 2012 › 11 › 21 › how-to-convert-a-multidimensional-array-to-an-object-in-php
How to convert a multidimensional array to an object in PHP – Life in apps, OSs and code!
November 21, 2012 - Today I was working on creating ... due to PHP’s stdClass class this can be easily achieved like so:- ... However, problems occur as I found out today that when trying to create an object from a multidimensional array only the top level of the array is converted to an object ...