3 Ways – Convert a PHP Object to an Array and Vice-Versa

Objects and arrays are essential parts of our day to day programming. PHP object to array and array to object conversions are quite common requirements. We are mentioning three ways that you can use to convert PHP object to an array.

Vice-versa, if you’re seeking for the reverse procedure, i.e. converting an array to object then here is the link of the article. There are methods for the array to object conversion that work on a multidimensional array as well.

Convert a PHP Object to an Array

1. Typecasting Object to Array

Either object to array or array to object conversion, typecasting is the easiest solution if the input is well-structured. By well-structured means, the input has valid keys. Below is the code to typecast and convert the object to the array.

2. Convert PHP Object to Array with JSON Functions

PHP’s JSON functions can also do the object to the array or vice-versa conversion smartly. Additionally, it works perfectly with nested objects to convert them as an associative array. This is the best solution if you want a full depth recursive conversion.

First, the json_encode() function converts the object into JSON string. Further, the second parameter in json_decode() function tells PHP to convert the encoded string to an associative array.

Despite the way you use to convert PHP object to the array, also take care of a few things. For a smooth conversion, always:

  • Avoid creating StdClass object with integer properties. They become quite inaccessible even you can see them using print_r() or similar.
  • Declare objects as public members of the class. Otherwise, the array keys will have weird and dirty notations. You can check about them at the official website for PHP.

3. Object to Array Conversion using get_object_vars()

Turning an object to an array using get_object_vars() is a lesser-known yet a quite good method. Also, the popular blogging platform WordPress uses it heavily. A good example of the object to array conversion is given below:

So you see that the get_object_vars() function returns an associative array of a defined object accessible in the scope. Also, it doesn’t take non-static properties for the specified object in the account. Additionally, if a property contains no value, it will be returned with a NULL value.

Other Ways for Converting PHP Object to Array

Finally, those above are the 3 preferred ways we wanted to share to convert a PHP object to an array. Indeed, there are other ways too for the same. Object iteration through a foreach loop or using PHP’s Reflection API and recursive function calls are a few of them.

However, we haven’t discussed them here in details because personally, we don’t like long lines of code for general needs. Still, they might be useful in hacky cases. So it’s worth to provide the code for them.

So here the post overs. Do let us know what is your preferred way to convert an object to an array in PHP. Please use the comment form to update us with your feedback.

You Might Interested In

Leave a Reply

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