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.
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.
1 | $array = (array) $object; |
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.
1 | $array = json_decode(json_encode(object), true); |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | class colors { public $red = 'ff0000'; private $green = '00f00'; protected $blue = '0000ff'; public $black, $white; static $rainbow; public function get_object_as_array() { return get_object_vars($this); } } $clrs = new colors; var_dump(get_object_vars($clrs)); /* array(3) { ["red"]=> string(6) "ff0000" ["black"]=> NULL ["white"]=> NULL } */ $arr = $clrs->get_object_as_array(); var_dump($arr); /* array(5) { ["red"]=> string(6) "ff0000" ["green"]=> string(5) "00f00" ["blue"]=> string(6) "0000ff" ["black"]=> NULL ["white"]=> NULL } */ |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | // Manual conversion using recursive function. function objectToArray($obj) { $arr = array(); foreach ($obj as $k => $v) $a[$k] = (is_array($v) || is_object($v)) ? objectToArray($v) : $v; return $arr; } /* Reflection API, getter and setter for object to array conversion. Modifiies the visibility of the property. */ function object_to_array_reflection($obj) { $rfc = new ReflectionClass(get_class($obj)); $arr = array(); foreach ($rfc->getProperties() as $prop) { $prop->setAccessible(true); $arr[$prop->getName()] = $prop->getValue($obj); $prop->setAccessible(false); } return $arr; } // Another custom function using get_object_vars(). function object2Array($obj) { if (is_object($obj)) { $obj = get_object_vars($obj); } // Use __METHOD__ instead of __FUNCTION__ if using the function inside a class. return (is_array($obj) ? array_map(__FUNCTION__, $obj) : $obj); } |
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.