How to Convert a PHP Object to an Associative Array: 3 Working Methods

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

From Object to Array: Why It's Needed and How to Avoid Pitfalls

\n\n

Every PHP developer sooner or later faces the task: there is an object (for example, from an API, database, or library), but a regular associative array is needed. It seems like a basic operation, but the devil, as always, is in the details. Incorrect conversion can lead to data loss, unexpected types, or fatal errors.

\n\n

In this article, we will break down three main approaches to converting a PHP object to an associative array, compare their performance, depth of processing, and pitfalls. You will learn which method to choose for 90% of tasks, and which is better left for laboratory experiments. Let's go!

\n\n

Method 1: Magic Type Casting (array)

\n\n

The most obvious and concise way is to use type casting: (array) $object. This method is built into PHP and works "out of the box". Let's look at an example.

\n\n
<?php\n$user = new stdClass();\n$user->name = 'Alexey';\n$user->age = 28;\n$user->email = 'alex@example.com';\n\n$array = (array) $user;\nprint_r($array);\n?>
\n\n

Result:

\n\n
Array\n(\n    [name] => Alexey\n    [age] => 28\n    [email] => alex@example.com\n)
\n\n

Looks perfect? Almost. But there are nuances.

\n\n

Pros of the (array) method

\n
    \n
  • Simplicity and speed — one operation, no loops.
  • \n
  • Works with any objects, including instances of custom classes.
  • \n
  • Preserves public properties of the object.
  • \n
\n\n

Cons and pitfalls

\n
    \n
  • Not recursive — if an object's property is itself an object, it will not be converted to an array. You will get a nested object inside the array.
  • \n
  • Problem with private/protected properties — they end up in the array, but with names containing null characters (\\0). This breaks serialization.
  • \n
  • Key changes — for built-in classes (DateTime, PDOStatement), behavior can be unpredictable.
  • \n
\n\n

Example of the problem with a private property:

\n\n
<?php\nclass User {\n    public $name = 'Ivan';\n    private $password = 'secret123';\n}\n\n$user = new User();\n$array = (array) $user;\nvar_dump(array_keys($array));\n// Output: array(2) { [0]=> string(4) "name" [1]=> string(17) "\\0User\\0password" }\n?>
\n\n

As you can see, the key \\0User\\0password is not something you'd want to see in your code.

\n\n

Method 2: JSON Bridge (json_decode + json_encode)

\n\n

The second popular approach is to first serialize the object to JSON, and then decode it into an array. This solves the problem of recursion and private properties, but adds overhead.

\n\n
<?php\n$object = new stdClass();\n$object->name = 'Maria';\n$object->skills = ['PHP', 'JavaScript', 'Python'];\n\n$array = json_decode(json_encode($object), true);\nprint_r($array);\n?>
\n\n

Result:

\n\n
Array\n(\n    [name] => Maria\n    [skills] => Array\n        (\n            [0] => PHP\n            [1] => JavaScript\n            [2] => Python\n        )\n)
\n\n

Now the nested array skills is also an array, not an object. Great!

\n\n

When this method helps

\n
    \n
  • Deeply nested structures — recursion is handled automatically.
  • \n
  • Data cleanliness — no special characters in keys.
  • \n
  • Compatibility — if you already work with JSON (API, databases), this is a natural choice.
  • \n
\n\n

Disadvantages of the JSON approach

\n
    \n
  • Loss of types — integers will remain integers, but objects like DateTime will be converted to strings. Objects with methods will lose their behavior.
  • \n

Blogs

Book Recommendations