How to Convert an Object to an Array in PHP (Including Nested Objects)

A cast handles simple objects, json_encode and json_decode handle nested ones, and WordPress has its own helpers. Here is when to use each, and the private property gotcha.

To convert a simple PHP object to an array, cast it: $array = (array) $object;. For nested objects, the quickest way is json_decode(json_encode($object), true), which converts every level at once. get_object_vars($object) returns only the properties visible from where you call it. In WordPress, WP_Post objects have a to_array() method, and wp_list_pluck() pulls one field out of a list of posts.

Originally published in January 2011 with a custom loop function. Rewritten in December 2026 with the built-in options.

Simple objects: cast to array

$user = new stdClass();
$user->id = 7;
$user->name = 'Ankur';

$array = (array) $user;
// ['id' => 7, 'name' => 'Ankur']

The cast only converts the top level. Objects inside stay objects.

Nested objects: json_encode and json_decode

$data = json_decode(json_encode($object), true);

Passing true to json_decode() returns associative arrays instead of objects, all the way down. This is the easiest option for API responses and config objects.

The trade-offs: only public properties are included, DateTime and other special objects become plain arrays or strings, and floats such as 1.0 may come back as 1. For value objects in your own code, add a toArray() method or implement JsonSerializable so you control the output.

Private and protected properties

An (array) cast includes private and protected properties, but their keys get odd prefixes with null bytes, such as "*email". That’s rarely what you want. get_object_vars() called from outside the class returns only public properties, which is usually the cleaner result:

$public = get_object_vars($object);

Called inside the class, get_object_vars($this) returns everything, which makes it a handy way to write toArray():

public function toArray(): array
{
    return get_object_vars($this);
}

Arrays of objects

To convert a list of objects, map over it:

$rows = array_map(fn ($o) => (array) $o, $objects);

WordPress

get_posts() returns WP_Post objects. Usually you don’t need to convert them at all:

// Only IDs
$ids = get_posts(['numberposts' => 10, 'fields' => 'ids']);

// One field from each post
$titles = wp_list_pluck(get_posts(['numberposts' => 10]), 'post_title');

// A single post as an array
$array = get_post(42)->to_array();

get_post($id, ARRAY_A) also returns the post as an associative array.

Common questions

What’s the difference between (array) and get_object_vars()?

The cast includes every property, with mangled keys for private and protected ones. get_object_vars() respects visibility, so from outside the class you get public properties only.

How do I convert an array back to an object?

Cast it: (object) $array. For nested arrays, use json_decode(json_encode($array)) without the true flag.

Why does json_encode return false?

Usually because of invalid UTF-8 or recursive references. Check json_last_error_msg(), or pass JSON_THROW_ON_ERROR to get an exception.