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

How to convert an array to object in PHP? - Stack Overflow
How can I convert an array like this to an object? [128] => Array ( [status] => "Figure A. Facebook's horizontal scrollbars showing up on a 1024x768 screen resolution."... More on stackoverflow.com
🌐 stackoverflow.com
php - How do I convert an object to an array? - Stack Overflow
If your array contains some other encoding, let say Win1250, it will fail, as json_encode will fail (php 5.3) 2013-12-03T14:40:05.633Z+00:00 ... @Trix use json again json_decode(json_encode($array), FALSE);This also (recursively) converts all of your sub arrays into objects, 2017-01-20T12:... More on stackoverflow.com
🌐 stackoverflow.com
How to turn array to object in PHP

Do it in a loop. Just to make sure it is 100% ready.

Make the computer play telephone ;)

More on reddit.com
🌐 r/ProgrammerHumor
2
47
November 21, 2020
Filtering JSON array by date
Try replacing your filter function with this: var resultProductData = product_data.filter(function (product) { var date = new Date(product.ProductDateCreated); return (date >= startDate && date <= endDate); }); More on reddit.com
🌐 r/javascript
1
2
July 28, 2016
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 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
Why does get_object_vars() fail with non-public properties when learning how to convert object to array in PHP?
get_object_vars() only works with public properties, so when learning how to convert object to array in PHP with private or protected properties, it won’t include those properties. If you need to include non-public properties in the array, you can either use Reflection to access them or create getter methods in the class to expose those properties before 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
🌐
TutorialsPoint
tutorialspoint.com › convert-object-to-an-array-in-php
Convert object to an array in PHP.
July 30, 2019 - An object is an instance of a class that has properties and methods, while an associative array uses string keys to store key−value pairs. Converting objects to arrays allows easier data manipulation and serialization. The json_encode() function converts a PHP value to JSON string, and json_decode() converts it back to a PHP array when used with the second parameter set to true −
🌐
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 - For example, imagine you’re working on a PHP-based · CRM system for a small business. The system receives customer data as an object from a third-party API, and the customer’s details are relatively simple, such as their name, contact information, and status. In this case, typecasting allows you to quickly convert the object to an array, making it easier to display the data in a table or pass it to another function without the overhead of more complex methods.
🌐
GeeksforGeeks
geeksforgeeks.org › php › convert-an-object-to-associative-array-in-php
Convert an object to associative array in PHP - GeeksforGeeks
July 11, 2025 - An array that contains a string index is called an associative array. It stores element values in association with key values rather than in linear index order. ... The json_decode function accepts JSON encoded string and converts it into a PHP variable on the other hand json_encode returns a JSON encoded string for a given value. ... <?php class sample { /* Member variables */ var $var1; var $var2; function __construct( $par1, $par2 ) { $this->var1 = $par1; $this->var2 = $par2; } } // Creating the object $myObj = new sample(1000, "second"); echo "Before conversion: \n"; var_dump($myObj); // Converting object to associative array $myArray = json_decode(json_encode($myObj), true); echo "After conversion: \n"; var_dump($myArray); ?>
Find elsewhere
🌐
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
🌐
Delft Stack
delftstack.com › home › howto › php › php object to array
How to Convert PHP Object to Associative Array | Delft Stack
February 2, 2024 - Python ScipyPythonPython TkinterBatchPowerShellPython PandasNumpyPython FlaskDjangoMatplotlibDockerPlotlySeabornMatlabLinuxGitCCppHTMLJavaScriptjQueryPython PygameTensorFlowTypeScriptAngularReactCSSPHPJavaGoKotlinNode.jsCsharpRustRubyArduinoMySQLMongoDBPostgresSQLiteRVBAScalaRaspberry Pi ... Use the array Keyword to Typecast the StdClass’s Object to Convert Into an Associative Array in PHP...
🌐
STechies
stechies.com › convert-php-object-array
How to Convert PHP object to array
An object is a specimen of class, which users use to define an individual data structure within a PHP class. PHP supports the conversion of an object into an array using the typecasting method and JSON Decode and Encode method. In this article, you will have insights into how to convert an object into an array using PHP.
🌐
YouTube
youtube.com › watch
Converting PHP stdClass Objects to Arrays: Simplified Guide - YouTube
Summary: Learn how to convert PHP stdClass objects to arrays using built-in functions and techniques. Simplify your PHP development with these conversion met...
Published: August 27, 2024
Views: 112
🌐
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 - To cast an object to an array, you can simply write the array type within parentheses before the object to convert. When a PHP object is converted to an associative array, all the object properties become the array elements.
🌐
GitHub
ocramius.github.io › blog › fast-php-object-to-array-conversion
Fast PHP Object to Array conversion
<?php class Foo { public $bar = 'barValue'; protected $baz = 'bazValue'; private $tab = 'tabValue'; } $foo = new Foo(); $arrayFoo = (array) $foo; var_dump($arrayFoo); ... array(3) { ["bar"]=> string(8) "barValue" ["*baz"]=> string(8) "bazValue" ["Footab"]=> string(8) "tabValue" } Weird, so $baz is copied to array key '*baz' and $tab is copied to Footab...
🌐
PHPpot
phppot.com › php › php-object-to-array
PHP Object to Array Convert using JSON Decode - PHPpot
Then, the Student class instance is encoded to prepare object type data. The json_encode() function prepares the JSON object to supply it for decoding. The json_decode() converts the PHP object to array.
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));
?>
🌐
GitHub
gist.github.com › ann71727 › 7d51454ac5c06b33e62e1f429feb1b97
PHP Convert Object To Array · GitHub
PHP Convert Object To Array. GitHub Gist: instantly share code, notes, and snippets.
🌐
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. PHP program to convert an object to an array using json encode and json decode.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
GitHub
gist.github.com › victorbstan › 744478
recursively cast a PHP object to array · GitHub
See here https://www.php.net/manual/de/function.json-encode.php#115733 ... This will not be be applicable if the properties of the object you are trying to convert are declared as private
🌐
Position Is Everything
positioniseverything.net › home › php object to array: a comprehensive guide with examples
PHP Object to Array Conversion: A Complete and Easy-to-Follow Guide
December 29, 2025 - In simpler terms, it converts a variable from one data type to another either manually or automatically. The casting method is either done by a compiler such as Visual Studio Code or manually by the programmer. The example below shows the conversion of an object to an array in PHP using the type casting method:
🌐
Edureka
edureka.co › blog › convert-object-to-array-in-php
How To Convert Object To Array In PHP | PHP Fundamentals | Edureka
September 20, 2019 - Generally, we define a class once and then make many objects that belong to it. One of the data structures that stores one or more similar types of values in a single name is an array but associative array in PHP is something different from a simple PHP array. Associative arrays are generally used to store key-value pairs. In this article we will learn ‘How To Convert Object To Array In PHP?’