Convert a PHP object to an associative array

Convert a PHP object to an associative array

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Converting PHP Objects to Associative Arrays

When integrating an external API that delivers data as objects, but your application logic relies on array manipulation, you quickly encounter a friction point. The built-in json_encode function often handles this conversion implicitly, yet it strips away the dynamic property access and type information inherent in native objects. For scenarios where you need a direct structural transformation without serializing to JSON first, PHP provides a straightforward mechanism that behaves consistently across versions.

The most idiomatic approach is using the (array) cast operator on the object instance. This syntax instructs the interpreter to iterate over the object's public properties and map them into an associative array. Consider the following scenario:

class User {
    public string $name;
    public int $age;
}

$user = new User();
$user->name = 'Alice';
$user->age = 30;

$data = (array) $user;
// Resulting array: ['name' => 'Alice', 'age' => 30]

This method is efficient because it operates at the language level without requiring external dependencies. It preserves the type of values contained within the properties, which is crucial if your downstream logic expects strict typing or relies on specific value types rather than strings.

However, relying solely on this cast requires an understanding of PHP's property visibility rules. By default, the (array) cast only exposes public and protected properties. Private properties remain inaccessible through this conversion. If your API response object contains private fields that you need to process, you must explicitly handle them or refactor the class definition to make those fields accessible to the casting logic.

Another consideration involves dynamic properties. The cast operator will include any property attached at runtime, even if it was not defined in the class schema. This flexibility is often a double-edged sword. If your code expects a strict contract based on the class definition, you might inadvertently introduce unexpected keys into your array structure. For instance, if a library dynamically adds a __debugInfo property for debugging purposes, that key could appear in your resulting array unless filtered explicitly.

For more complex requirements, such as converting objects to arrays while preserving nested structures or handling specific naming conventions, the json_encode and json_decode combination is an alternative. While this involves an intermediate JSON string, it normalizes the object structure and can be useful when dealing with polymorphic data where the exact class hierarchy might vary.

If you are working within the Laravel ecosystem, there are utility services that abstract this conversion pattern. The framework provides mechanisms to access underlying array representations of Eloquent models or other domain objects without losing the ability to interact with them as arrays later. This is particularly relevant when migrating from API responses that define custom classes back into your application's internal data structures.

Ultimately, the choice between casting and serialization depends on performance constraints and strictness requirements. The (array) cast offers near-zero overhead and maintains type integrity for primitive types. It is the preferred method for quick-and-dirty conversions where you need immediate access to the data as an array without the overhead of JSON parsing. Just ensure you are aware of visibility rules and dynamic property behavior to avoid subtle bugs in your application flow.

Our network