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
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(k => $v) {
            if (is_integer($k)) {
                // only need this if you want to keep the array indexes separate
                // from the object notation: eg. a['index'][$k] = convert_array_to_obj_recursive($v);
            }
            else {
                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]). 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.

🌐
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).
🌐
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 ...
🌐
Medium
medium.com › laravelnepal › convert-your-deeply-nested-objects-to-array-50b3286bc323
Convert your deeply nested objects to array | by Suman Shrestha | laravelnepal | Medium
October 25, 2017 - Convert your deeply nested objects to array In php, you can convert nested object to array in many ways. You can loop through the properties. You can (array) $object to change. Or create an entirely …
🌐
If Not True Then False
if-not-true-then-false.com › 2009 › php-tip-convert-stdclass-object-to-multidimensional-array-and-convert-multidimensional-array-to-stdclass-object
PHP stdClass to Array and Array to stdClass – stdClass Object :: If Not True Then False
December 30, 2009 - This post defines two ultra simple recursive function to convert multidimensional Objects to Arrays and multidimensional Arrays to Objects. <?php function objectToArray($d) { if (is_object($d)) { // Gets the properties of the given object // with get_object_vars function $d = get_object_vars($d); } if (is_array($d)) { /* * Return array converted to object * Using __FUNCTION__ (Magic constant) * for recursive call */ return array_map(__FUNCTION__, $d); } else { // Return array return $d; } }
Find elsewhere
🌐
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.
🌐
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.
🌐
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
🌐
TutorialsPoint
tutorialspoint.com › How-to-convert-Multidimensional-PHP-array-to-JavaScript-array
How to convert Multidimensional PHP array to JavaScript array?
July 30, 2019 - It works for a single as well as the multidimensional array. Use the json_encode() method to achieve this. The following is the Multi-dimensional PHP array. $myArr= array( array('Amit', 'amit@example.com'), array('Rahul, 'rahul@example.com'), ); Converting PHP multidimensional array into JavaScript.
🌐
Rich Jenkins' Blog
richjenks.com › converting objects and arrays
Converting Objects and Arrays | Rich Jenkins' Blog
March 15, 2018 - Unidimensional arrays and objects can be converted by casting (e.g. $array = (array) $object; / $object = (object) $array;) but if multidimensional only the first tier is converted. Here’s the solution! As of PHP 5.4 you can use the short array syntax, which replaces array() with [] You could iterate through each array item / object property and cast it, then recursively check if it has children which need casting too, but PHP’s JSON functions offer an easier way.
🌐
Stack Overflow
stackoverflow.com › questions › 4345554 › convert-a-php-object-to-an-associative-array
Convert a PHP object to an associative array - Stack Overflow
function object_to_array(array|object $data) : array { $result = (array) $data; foreach ($result as $key => $value) { if (is_object($value)) $value = (array) $value; if (is_array($value)) $result[$key] = object_to_array($value); } return $result; ...
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-flatten-multidimentional-array-in-php
How to Flatten Multidimentional Array in PHP ? - GeeksforGeeks
July 11, 2024 - PHP's array_walk_recursive() function provides a straightforward way to flatten a multidimensional array.