Convert a PHP object to an associative array
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In PHP, objects can be converted to associative arrays using the get_object_vars() function or by casting the object with (array). This is particularly useful when you need to extract data from an object to use in functions that require arrays or for debugging purposes. Below, we explore each method, their differences, and implications, with practical examples providing a deeper understanding of the subject.
Using get_object_vars()
The get_object_vars() function returns an associative array of defined properties of an object that are visible to the current scope. It's important to note that if a property is private or protected, it won't be returned unless the function is called in the scope where the property is defined.
Example
Consider the following class:
Output:
In this example, only the public property $name is returned. The protected property $age and private property $phone are not included in the output.
Using (array) Casting
Casting an object to an array converts all visibility levels of an object’s properties into an associative array. The array keys preserve the visibility by prefixing properties' names with * for protected properties and the class name for private properties.
Example
Using the same Person class:
Output:
Here, all properties are converted, including protected and private ones, with keys indicating their visibility.
Differences and When to Use
| Feature | get_object_vars() | (array) Casting |
| Visibility | Only accessible properties | All properties, with prefixes |
| Output Structure | Simple associative array | Associative array with prefixes |
| Preferred Use | Public data extraction | Debugging or specific needs where visibility must be retained |
Considerations
- Type Compatibility: When converting objects to arrays, ensure your properties are compatible with array values, as objects in properties will remain as objects.
- Performance: The choice between
get_object_vars()and(array)casting may depend on specific performance considerations in the context of your app. - Visibility Handling: Choose the method based on whether property visibility needs to be preserved in the output.
Deep Conversion of Nested Objects
To convert an object along with its nested objects to an associative array, you will need to recursively apply the conversion. Here’s a function that handles this:
This will convert not only the top-level object but also any nested objects into associative arrays. This function uses recursion to delve deep into the object structure and convert it entirely.
Conclusion
Converting PHP objects to associative arrays can be implemented through either the get_object_vars() function or (array) casting, depending on the requirement of retaining property visibility and the scope of the properties you need to access. Each method is suitable for different scenarios in PHP programming, whether it's for data extraction for functional usage or debugging purposes.

