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);
🌐
GeeksforGeeks
geeksforgeeks.org › php › convert-an-object-to-associative-array-in-php
Convert an object to associative array in PHP - GeeksforGeeks
July 11, 2025 - The get_object_vars() function returns an associative array containing the properties of the given object. ... <?php class car { /* Member variables */ var $make; var $model; var $year; function __construct($make, $model, $year) { $this->make ...
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
How to convert an array with objects to PHP query string?
You can create an HTTPClient instance and call query_string_from_dict() on it to get a query string. This method doesn't seem to support multidimensional arrays though. These require a special treatment on the backend due to their nature anyway (you can't append them in form data, you have to specify their index explicitly). You'll have to implement your own method to do this. That said, using JSON to send data from a non-browser application may be a better idea :) More on reddit.com
🌐 r/godot
6
6
November 22, 2019
Why does PHP convert integer-string array keys to integer?
The thing is that PHP is used for web stuff, thus all input is coming as string, be it numeric or textual (or binary) data. Having those auto conversions makes that simple. Nowadays the language evolved and frameworks and type handling changed things, but such fundamental issues can not be changed without breaking "everything" Just a few examples what becomes simpler: Given a script like More on reddit.com
🌐 r/PHP
17
16
September 4, 2022
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
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
🌐
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 - Let's say you’re working with a PHP-based e-commerce platform like Shopify, where customer data is retrieved via an API in object format. You need to manipulate this data, maybe to display it on a dashboard or process it for analysis, but it's much easier to work with arrays.
🌐
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.
Find elsewhere
🌐
GitHub
gist.github.com › victorbstan › 744478
recursively cast a PHP object to array · GitHub
ArrayObject::getArrayCopy() return shallow copy of array. ... @scmrus and @c01nd01r I actually understood this after trying it, but I forgot to remove the comment from here. Sorry. BTW, a recursive function in PHP is faster than the JSON way. ... Be care full if use encodings other than UTF-8. If you use that, json_encode will return false. 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
🌐
TutorialsPoint
tutorialspoint.com › article › convert-object-to-an-array-in-php
Convert object to an array in PHP.
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; ...
🌐
Facebook
facebook.com › educba.official › posts › struggling-with-converting-php-objects-to-arrays-this-quick-guide-explains-simpl › 1374665454686837
Struggling with converting PHP objects to arrays? 🤔 This ...
Educba. 72,752 followers · 11 talking about this. EDUCBA teaches you real world skills on everything from Investment Banking to Programming to Project Management to Design & many more
🌐
W3Schools
w3schools.com › PhP › php_arrays.asp
PHP Arrays
In PHP, an array is a special variable that can hold many values under a single name, and you can access the values by referring to an index number or a name. An array stores multiple values in one single variable: $cars = array("Volvo", "BMW", "Toyota"); Try it Yourself » ... Array items can be of any data type. The most common are strings and numbers, but array items can also be objects, functions or even arrays.
🌐
Rich Jenkins' Blog
richjenks.com › converting objects and arrays
Converting Objects and Arrays | Rich Jenkins' Blog
March 15, 2018 - As of PHP 5.4 you can use the short array syntax, which replaces array() with [] You could iterate through each array item / object property and cast it, then recursively check if it has children which need casting too, but PHP’s JSON functions offer an easier way.
🌐
Bobbyallen
blog.bobbyallen.me › 2012 › 11 › 21 › how-to-convert-a-multidimensional-array-to-an-object-in-php
How to convert a multidimensional array to an object in PHP – Life in apps, OSs and code!
November 21, 2012 - Today I was working on creating an object from an array of data, I didn’t want a full blown class as I was only accessing the data as application settings and thankfully due to PHP’s stdClass class this can be easily achieved like so:-
🌐
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 - Typecasting is a technique in which one data type variable into the another data type. It is considered as an explicit data type conversion. It can translate a PHP object to an array with the help of typecasting rules in PHP.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Laracasts
laracasts.com › discuss › channels › laravel › how-to-convert-an-array-to-object-in-php
How to convert an array to object in PHP
Another option would be to instantiate a standard class as a variable, and loop through your array while re-assigning the values: $object = new stdClass(); foreach ($array as $key => $value) { $object->$key = $value; }
🌐
Wyzant
wyzant.com › resources › ask an expert
How to convert an array to object in PHP? | Wyzant Ask An Expert
May 6, 2019 - Php · Asked • 05/06/19 · How can i convert an array like this to object? <pre> [128] => Array ( [status] => Figure A. Facebook's horizontal scrollbars showing up on a 1024x768 screen resolution. ) [129] => Array ( [status] => The other day at work, I had some spare time ) ) </pre> Follow • 1 ·
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-convert-an-array-into-object-using-stdclass-in-php
How to convert an array into object using stdClass() in PHP? - GeeksforGeeks
July 11, 2025 - ... <?php // Function to convert ... into // stdClass object foreach ($Array as $key => $value) { if (is_array($value)) { $value = ToObject($value); } $object->$key = $value; } return $object; } // D...