Convert an object to an array in PHP
This article demonstrates how to convert an object to an array in PHP.
1. Using Type Casting
A simple option to convert an object to an associative array is typecasting. To cast an object to an array, you can simply write the array type within parentheses before the object to convert. When a PHP object is converted to an associative array, all the object properties become the array elements. For example, the following solution casts a StdClass object to an array:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $obj = new StdClass; $obj->x = 0; $obj->y = 0; $array = (array) $obj; var_export($array); /* Output: array ( 'x' => 0, 'y' => 0, ) */ ?> |
The following solution typecasts an object having only public properties.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php class Point { public $x, $y; public function __construct() { $this->x = 0; $this->y = 0; } } $array = (array) new Point; var_export($array); /* Output: array ( 'x' => 0, 'y' => 0, ) */ ?> |
As evident from the above examples, type casting works well with StdClass and class for all public properties. If your object contains any non-public fields, the array keys will include the visibility scope. The private and protected properties will have the class name and '*' prepended to the element name, respectively. Note that the class name and '*' are separated by the null character ("\0") on both sides, as illustrated 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 |
<?php class Point { public $x; protected $y; private $z; public function __construct() { $this->x = 0; $this->y = 0; $this->z = 0; } } $array = (array) new Point; var_export($array); /* Output: array ( 'x' => 0, '' . "\0" . '*' . "\0" . 'y' => 0, '' . "\0" . 'Point' . "\0" . 'z' => 0, ) */ ?> |
2. Using get_object_vars() function
Alternatively, you may use the get_object_vars() function to get an associative array of accessible non-static properties of the specified object according to scope. Note that the private and protected properties in the object will be ignored, if this function is called from within the scope of the object.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $obj = new StdClass; $obj->x = 0; $obj->y = 0; $array = get_object_vars($obj); var_export($array); /* Output: array ( 'x' => 0, 'y' => 0, ) */ ?> |
3. Using Reflection
You may use Reflection to access private and protected fields outside the object’s scope. This example uses ReflectionClass::getProperties() to retrieve reflected properties and stores them in an array. Unlike type casting, this solution results in proper key names for non-public fields. Before PHP 8.1.0, you must call ReflectionProperty::setAccessible() to enable access to a protected or private property. As of PHP 8.1.0, all properties are accessible by default.
|
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 |
<?php function access_non_public_fields($obj) { $reflect = new ReflectionClass($obj); $props = $reflect->getProperties(); $array = array(); foreach ($props as $prop) { $prop->setAccessible(true); $array[$prop->getName()] = $prop->getValue($obj); $prop->setAccessible(false); } return $array; } class Point { public $x; protected $y; private $z; public function __construct() { $this->x = 0; $this->y = 0; $this->z = 0; } } $array = access_non_public_fields(new Point); var_export($array); /* Output: array ( 'x' => 0, 'y' => 0, 'z' => 0, ) */ ?> |
4. Using json_encode() and json_decode() function
If your object only has public attributes, you can serialize it to JSON and then deserialize it. You can convert the object into a string containing the JSON representation with json_encode(), and then convert the JSON string into an associative array using the json_decode() function with its second parameter set to true.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $obj = new StdClass; $obj->x = 0; $obj->y = 0; $array = json_decode(json_encode($obj), true); var_export($array); /* Output: array ( 'x' => 0, 'y' => 0, ) */ ?> |
Since json_encode() function is recursive, an object will be serialized recursively. For example,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php $origin = new StdClass; $origin->x = 0; $origin->y = 0; $obj = new StdClass; $obj->coordinate = $origin; $array = json_decode(json_encode($obj), true); var_export($array); /* Output: array ( 'coordinate' => array ( 'x' => 0, 'y' => 0, ), ) */ ?> |
5. Using foreach loop
Finally, you can write a recursive procedure to convert an object to a multidimensional array using a foreach loop, as shown below. This option is available for objects with all public properties. Any private and protected properties in the object won’t be converted.
|
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 |
<?php function to_array($data) { if (!is_object($data)) { return $data; } $array = array(); foreach ($data as $key => $val) { $array[$key] = to_array($val); } return $array; } $origin = new StdClass; $origin->x = 0; $origin->y = 0; $obj = new StdClass; $obj->coordinate = $origin; $array = to_array($obj); var_export($array); /* Output: array ( 'coordinate' => array ( 'x' => 0, 'y' => 0, ), ) */ ?> |
That’s all about converting an object to an array in PHP.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)