Just typecast it

$array = (array) $yourObject;

From Arrays:

If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side.

Example: Simple Object

$object = new StdClass;
$object->foo = 1;
$object->bar = 2;

var_dump( (array) $object );

Output:

array(2) {
  'foo' => int(1)
  'bar' => int(2)
}

Example: Complex Object

class Foo
{
    private $foo;
    protected $bar;
    public $baz;

    public function __construct()
    {
        $this->foo = 1;
        $this->bar = 2;
        $this->baz = new StdClass;
    }
}

var_dump( (array) new Foo );

Output (with \0s edited in for clarity):

array(3) {
  '\0Foo\0foo' => int(1)
  '\0*\0bar' => int(2)
  'baz' => class stdClass#2 (0) {}
}

Output with var_export instead of var_dump:

array (
  '' . "\0" . 'Foo' . "\0" . 'foo' => 1,
  '' . "\0" . '*' . "\0" . 'bar' => 2,
  'baz' =>
  stdClass::__set_state(array(
  )),
)

To know more about php built-in class stdclass. You can read this documentation

Typecasting this way will not do deep casting of the object graph and you need to apply the null bytes (as explained in the manual quote) to access any non-public attributes. So this works best when casting StdClass objects or objects with only public properties. For quick and dirty (what you asked for) it's fine.

Also see this in-depth blog post:

  • Fast PHP Object to Array conversion
Answer from Gordon on Stack Overflow
Top answer
1 of 16
1825

Just typecast it

$array = (array) $yourObject;

From Arrays:

If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side.

Example: Simple Object

$object = new StdClass;
$object->foo = 1;
$object->bar = 2;

var_dump( (array) $object );

Output:

array(2) {
  'foo' => int(1)
  'bar' => int(2)
}

Example: Complex Object

class Foo
{
    private $foo;
    protected $bar;
    public $baz;

    public function __construct()
    {
        $this->foo = 1;
        $this->bar = 2;
        $this->baz = new StdClass;
    }
}

var_dump( (array) new Foo );

Output (with \0s edited in for clarity):

array(3) {
  '\0Foo\0foo' => int(1)
  '\0*\0bar' => int(2)
  'baz' => class stdClass#2 (0) {}
}

Output with var_export instead of var_dump:

array (
  '' . "\0" . 'Foo' . "\0" . 'foo' => 1,
  '' . "\0" . '*' . "\0" . 'bar' => 2,
  'baz' =>
  stdClass::__set_state(array(
  )),
)

To know more about php built-in class stdclass. You can read this documentation

Typecasting this way will not do deep casting of the object graph and you need to apply the null bytes (as explained in the manual quote) to access any non-public attributes. So this works best when casting StdClass objects or objects with only public properties. For quick and dirty (what you asked for) it's fine.

Also see this in-depth blog post:

  • Fast PHP Object to Array conversion
2 of 16
540

You can quickly convert deeply nested objects to associative arrays by relying on the behavior of the JSON encode/decode functions:

$array = json_decode(json_encode($nested_object), true);
Discussions

php - How do I convert an object to an array? - Stack Overflow
For example, get_object_vars will ... function of the object). (array), on the other hand, will cast to an array with all public, private and protected members intact on the array, though all public now, of course. A somewhat dirty method is to use PHP >= 5.2's native JSON ... More on stackoverflow.com
🌐 stackoverflow.com
php - Casting object to array - any magic method being called? - Stack Overflow
It's strange he should say so, cause actually php has an object to array convertion: php.net/manual/en/… 2014-01-21T15:03:46.713Z+00:00 ... The question is actually about being able to implement custom casting logic. More on stackoverflow.com
🌐 stackoverflow.com
How to convert (cast) Object to Array without Class Name prefix in PHP? - Stack Overflow
How to convert (cast) Object to Array without Class Name prefix in PHP? class Teste{ private $a; private $b; function __construct($a, $b) { $this->a = $a; $this-&g... More on stackoverflow.com
🌐 stackoverflow.com
Yet another array-to-object converter
tbh could be 1 file, 2 functions. More on reddit.com
🌐 r/PHP
3
9
September 17, 2023
People also ask

How can I convert an array back into an object in PHP?
You can easily convert an array back to an object in PHP by casting the array to an object using typecasting, like this: $object = (object) $array;. This method is particularly useful when you need to work with data in an object-oriented way, such as when manipulating data within classes and methods. For nested arrays, JSON encoding/decoding is also an option to achieve the reverse conversion.
🌐
upgrad.com
upgrad.com › home › blog › software development › how to convert object to array in php? techniques and examples
How to Convert Object to Array in PHP – Quick & Simple Methods
Can I convert an object to an array in PHP while maintaining its methods?
No, converting an object to an array using methods like typecasting or get_object_vars() will only extract the object’s properties, not its methods. If you need to keep track of the methods alongside the properties, you would have to manage them separately or use serialization, which includes both data and methods, although it can’t directly preserve methods in an array.
🌐
upgrad.com
upgrad.com › home › blog › software development › how to convert object to array in php? techniques and examples
How to Convert Object to Array in PHP – Quick & Simple Methods
Can I use JSON encoding/decoding for all objects when learning how to convert object to array in PHP?
While JSON encoding/decoding is versatile, it isn't suitable for all objects when learning how to convert object to array in PHP. For instance, it doesn’t work well with resources (like file handles or database connections) or objects with non-UTF8 characters. Always check the compatibility of your object before using JSON methods, especially when dealing with special data types or large resources.
🌐
upgrad.com
upgrad.com › home › blog › software development › how to convert object to array in php? techniques and examples
How to Convert Object to Array in PHP – Quick & Simple Methods
🌐
GitHub
ocramius.github.io › blog › fast-php-object-to-array-conversion
Fast PHP Object to Array conversion
I actually spent some time trying to understand why this was happening, and even the debugger was failing me! Then I tried using var_export: ... array ( 'bar' => 'barValue', '' . "\0" . '*' . "\0" . 'baz' => 'bazValue', '' . "\0" . 'Foo' . "\0" . 'tab' => 'tabValue', ) Null characters are used as delimiters between the visibility scope of a particular property and its name! That's some really strange results, and they give us some insight on how PHP actually keeps us from accessing private and protected properties.
🌐
Upgrad
upgrad.com › home › blog › software development › how to convert object to array in php? techniques and examples
How to Convert Object to Array in PHP – Quick & Simple Methods
September 10, 2025 - You can easily convert an array back to an object in PHP by casting the array to an object using typecasting, like this: $object = (object) $array;. This method is particularly useful when you need to work with data in an object-oriented way, ...
🌐
Medium
medium.com › @mavelar › convert-array-to-object-object-to-array-in-php-e430ae492951
Convert Array to Object & Object to Array in PHP | by Manuel Avelar | Medium
September 28, 2017 - If you ever need to change an Object into an Array, then use the (array) type casting function. function object_to_array($object) { return (array) $object; }
🌐
GitHub
gist.github.com › victorbstan › 744478
recursively cast a PHP object to array · GitHub
Use ArrayObject class and cast it to array (or use ArrayObject::getArrayCopy() instead). It's much much faster. ArrayObject::getArrayCopy() return shallow copy of array. ... @scmrus and @c01nd01r I actually understood this after trying it, but ...
🌐
EDUCBA
educba.com › home › software development › software development tutorials › php tutorial › php object to array
PHP object to array | How to Convert object to array in PHP with example?
April 18, 2023 - Once this is done, the constructor takes parameters that are later offered during the object creation using the keyword “new”. From the program, it can be seen that objects get printed in the first case of expression var_dump(). But in the second case of expression, an object is casted into an array using the typecasting procedure. Here, the first method in the method sections is used for converting an object into an array. An associative array is an array that consists of a string index which stores item values linked with key values other than in order of the linear index. This article saw how PHP object to array is working, methods to achieve it, and different examples.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Find elsewhere
🌐
Exakat
exakat.io › home › mastering the (array) cast operator in php
Mastering the (array) Cast Operator in PHP - Exakat
December 3, 2023 - The (array) cast operator is a simple and convenient way to convert different data types into an array. It is especially useful when working with data that are not yet in array format, whether you’re dealing with scalar values, objects, or ...
🌐
GeeksforGeeks
geeksforgeeks.org › php › convert-an-object-to-associative-array-in-php
Convert an object to associative array in PHP - GeeksforGeeks
July 11, 2025 - Before conversion: object(sample)#1 ... conversion of a data type. It can convert a PHP object to an array by using typecasting rules supported in PHP....
Top answer
1 of 11
396

Single-dimensional arrays

For converting single-dimension arrays, you can cast using (array) or there's get_object_vars, which Benoit mentioned in his answer.

// Cast to an array
$array = (array) $object;
// get_object_vars
$array = get_object_vars($object);

They work slightly different from each other. For example, get_object_vars will return an array with only publicly accessible properties unless it is called from within the scope of the object you're passing (ie in a member function of the object). (array), on the other hand, will cast to an array with all public, private and protected members intact on the array, though all public now, of course.

Multi-dimensional arrays

A somewhat dirty method is to use PHP >= 5.2's native JSON functions to encode to JSON and then decode back to an array. This will not include private and protected members, however, and is not suitable for objects that contain data that cannot be JSON encoded (such as binary data).

// The second parameter of json_decode forces parsing into an associative array
$array = json_decode(json_encode($object), true);

Alternatively, the following function will convert from an object to an array including private and protected members, taken from here and modified to use casting:

function objectToArray ($object) {
    if(!is_object($object) && !is_array($object))
        return $object;

    return array_map('objectToArray', (array) $object);
}
2 of 11
138

You should look at get_object_vars , as your properties are declared private you should call this inside the class and return its results.

Be careful, for primitive data types like strings it will work great, but I don't know how it behaves with nested objects.

in your case you have to do something like;

<?php
   print_r(get_object_vars($response->response->docs));
?>
🌐
Like Geeks
likegeeks.com › home › web development › cast or convert an array to object using php (hydrator pattern)
Cast or Convert an Array to Object Using PHP (Hydrator Pattern)
Convert an Array To Object Using PHP Hydrator Pattern and Object To Array All By PHP Reflection Functions and Pattern Matching For Getters and Setters
🌐
W3Schools
w3schools.com › php › php_casting.asp
PHP Type Casting
When casting to array, most data types converts into an indexed array with one element. NULL values converts into an empty array object.
🌐
Squash
squash.io › how-to-convert-a-php-object-to-an-associative-array
How To Convert A Php Object To An Associative Array
One straightforward way to convert an object to an associative array is to use typecasting. PHP allows you to cast an object to an array, which will create an associative array where the object properties become the keys, and their values become ...
🌐
Fellow Tuts
fellowtuts.com › home › php › 3 ways – convert a php object to an array and vice-versa
3 Ways - Convert a PHP Object to an Array and Vice-Versa
December 21, 2018 - Either object to array or array to object conversion, typecasting is the easiest solution if the input is well-structured. By well-structured means, the input has valid keys. Below is the code to typecast and convert the object to the array. ... PHP’s JSON functions can also do the object to the array or vice-versa conversion smartly.
🌐
TutorialsPoint
tutorialspoint.com › convert-object-to-an-array-in-php
Convert object to an array in PHP.
July 30, 2019 - Typecasting with (array) directly converts an object to an array. This method is faster and more straightforward − · <?php class Bag { public function __construct($item1, $item2, $item3) { $this->item1 = $item1; $this->item2 = $item2; ...
🌐
Techie Delight
techiedelight.com › home › java › convert an object to an array in php
Convert an object to an array in PHP | Techie Delight
July 7, 2026 - 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.