3 Ways – Multidimensional Array to Object Conversion

There are few ways to convert a multidimensional array to object. If you’re a PHP developer then you have noticed that array to object conversion as a common need. So we’re mentioning three methods to convert a multidimensional or simple associative array to object.

Like you, we’re also not a fan of typing braces and quotes around array keys. It makes us hit extra keystrokes. Further, using array doesn’t let the code stay neat and better readable as compared to objects. Cause of these reasons, we always prefer the object over an array.

Multidimensional Array to Object Conversion

Cast Non-Nested Array to Object

This is the most preferred way to convert an array to object. 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.

1. Multidimensional Array to Object Conversion using JSON Encode/Decode

A combination of PHP’s JSON encode and decode functions can magically do the conversion.

First, the json_encode($array) converts the entire multi-dimensional array to a JSON string. Then the json_decode($string) will convert the JSON string to a stdClass object.

Some developers claim that this method has performance concern. However, we don’t have any benchmark result yet to test the same. Despite that, we extensively use JSON functions for object conversion.

Also, check how to change array key without changing the order in PHP.

2. Convert Multidimensional Array to Object using array_map

This one is an object casting along with PHP’s array_map() and __FUNCTION__ magic constant. Also, know that the return statement must contain ternary condition else the conversion will fail. Additionally, use __METHOD__ instead of __FUNCTION__ if you’re using the function inside a class.

If you have a large array of objects then this article can help you find the object by value. You can also extract an array from a set of matched/excluded keys.

3. Custom Function to Convert Multi-dimensional Array to Object

Here is an additional function for a multi-dimensional array to object conversion. Further, this is useful if the array is a mixed type. Like it has indexed keys at the top level or further levels. Check the function and the example uses:

Here we are moving all indexed keys under a property named ‘index’ and then converting the array to object. We did so because neither the notation $obj->1 nor $obj[0] works as well as both throw PHP error. But the object notation $obj->index{0} returns the expected value.

Finally, those are three different ways to convert a multidimensional array to object. The first one is indeed suitable for a simple associative array. The next two are perfect ways for a multidimensional array to object conversion. However, the last one gives you more flexibility to operate a mixed typed array as an object.

Also, there might be other methods or variation of the last one for the same. We wish to know about them. Do you have any or have any concern regarding PHP arrays and objects? Do let us know via the comment form given.

You Might Interested In

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.