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 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
Converting python dictionary into something php can use? Associative Array?

I would suggest using JSON, the whole point of it is to make communicating between computers/devices/software/languages easier...so, take the help :D

I dunno how to do it on Py end, but with PHP you would just use json_decode (second param is for result of json_decode to be assoc. array)

 foreach (json_decode($output, true) as $item) {
More on reddit.com
🌐 r/PHPhelp
5
5
February 20, 2017
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
🌐
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 - // Echoing a PHP Array value echo $array['value']; // Echoing a PHP Object value echo $object->value; Now to the conversion (casting) of a PHP Array into a PHP Object. This is very simple.
🌐
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.
🌐
PHP
php.net › manual › en › function.get-object-vars.php
PHP: get_object_vars - Manual
That which seems to be spaces into array keys are '\0' characters, so the general rule to parse keys seems to be: Public members: member_name Protected memebers: \0*\0member_name Private members: \0Class_name\0member_name I've wroten a obj2array function that creates entries without visibility for each key, so you can handle them into the array as it were within the object: <?php function obj2array ( &$Instance ) { $clone = (array) $Instance; $rtn = array (); $rtn['___SOURCE_KEYS_'] = $clone; while ( list ($key, $value) = each ($clone) ) { $aux = explode ("\0", $key); $newkey = $aux[count($aux)-1]; $rtn[$newkey] = &$rtn['___SOURCE_KEYS_'][$key]; } return $rtn; } ?> I've created also a <i>bless</i> function that works similar to Perl's bless, so you can further recast the array converting it in an object of an specific class: <?php function bless ( &$Instance, $Class ) { if ( !
Find elsewhere
🌐
Jsontophp
jsontophp.com
Convert JSON Object to PHP Array Online
JSON to PHP array converter online - Convert the given JSON object or Array into beautified PHP array that can be used instantly into your PHP file as a PHP array
🌐
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
🌐
Rich Jenkins' Blog
richjenks.com › converting objects and arrays
Converting Objects and Arrays | Rich Jenkins' Blog
March 15, 2018 - Unidimensional arrays and objects can be converted by casting (e.g. $array = (array) $object; / $object = (object) $array;) but if multidimensional only the first tier is converted. Here’s the solution! As of PHP 5.4 you can use the short array syntax, which replaces array() with [] You could ...
🌐
W3Schools
w3schools.com › php › php_casting.asp
PHP Type Casting
If a value is 0, NULL, false, or empty, (bool) converts it to false, otherwise true. Even -1 converts to true. To cast to data type array: Use (array) before the variable or value to convert:
🌐
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.
🌐
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
🌐
Laracasts
laracasts.com › discuss › channels › laravel › how-to-convert-an-array-to-object-in-php
How to convert an array to object in PHP
Hi everyone, i have a probrem for convert array to object, but the format is still an array in an array class ReportExport implements FromCollection { public function collection() { $cross_kehadiran[] = DB::table('attendance as in') ->where('in.in_out', 'in') ->where('in.attendance_location_id', $locations->id)
🌐
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...
🌐
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 ... due to PHP’s stdClass class this can be easily achieved like so:- ... However, problems occur as I found out today that when trying to create an object from a multidimensional array only the top level of the array is converted to an object ...
🌐
Laravel
laravel.com › docs › 12.x › collections
Collections | Laravel 12.x - The clean stack for Artisans and agents
Laravel is a framework for building modern web apps and AI agents. Expressive syntax, built-in tools, and everything you need to ship fast.
🌐
Laravel Plug
laravelplug.com › home › php object to array conversion – 2 easy methods
PHP Object to Array Conversion - 2 Easy Methods - Laravel Plug
February 28, 2024 - In PHP, we can convert an object to an array using typecasting or by using built-in functions like get_object_vars() or json_decode().