The easiest way is to JSON-encode your object and then decode it back to an array:

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

Or if you prefer, you can traverse the object manually, too:

foreach ($object as $value) 
    $array[] = $value->post_id;
Answer from Amal on Stack Overflow
🌐
Strangebuzz
strangebuzz.com › home › snippets › converting a php stdclass object into an array
Converting a PHP stdClass object into an array | Strangebuzz
*/ trait Snippet311Trait { public function snippet311(): void { $stdClass = new \stdClass(); $stdClass->foo = 'bar'; $stdClass->object_property = new \stdClass(); $stdClass->object_property->foo2 = 'bar2'; echo '----- Nominal case OK ✅'.PHP_EOL; echo '> original object:'.PHP_EOL; $array = json_decode((string) json_encode($stdClass), true); var_dump($stdClass); echo '> as array:'.PHP_EOL; var_dump($array); echo PHP_EOL; // With a cast, it doesn't work as the object_property is not converted into an array echo '----- cast case, does not work ❌'.PHP_EOL; var_dump((array) $stdClass); // That's it! 😁 } } Run this snippet More on Stackoverflow Read the doc Random snippet · Work with me! ... Did you like this post? You can help me back in several ways: (use the "reply" link on the right to comment or to contact me )
Discussions

php - How to convert an array into an object using stdClass() - Stack Overflow
I have made the following array: $clasa = array( 'e1' => array('nume' => 'Nitu', 'prenume' => 'Andrei', 'sex' => 'm', 'varsta' => 23), 'e2' => array('nume' => ... More on stackoverflow.com
🌐 stackoverflow.com
php - stdClass to array? - Stack Overflow
If i use yout solution this working only for current array - children array still are stdClass 2012-07-09T13:52:06.513Z+00:00 ... Yes, the children will still be stdClass objects, you'll have to cast them as well. 2012-07-09T13:56:34.15Z+00:00 ... If you're using json_decode to convert that JSON ... More on stackoverflow.com
🌐 stackoverflow.com
November 25, 2015
how to convert stdclass object to array in php and javascript - Stack Overflow
you can perform this in a loop to get a multi-dimensional array of the StdClass objects you have in an array. ... Sign up to request clarification or add additional context in comments. ... M. Fahmi Ulul Azmi · M. Fahmi Ulul Azmi Over a year ago · thats only work on php. More on stackoverflow.com
🌐 stackoverflow.com
April 29, 2017
Convert stdClass object to associative array in php - Stack Overflow
Where $items is the name of your array. Should go through your array of objects, convert that object to an associative array, and merge it into the $final_array More on stackoverflow.com
🌐 stackoverflow.com
🌐
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
🌐
If Not True Then False
if-not-true-then-false.com › 2009 › php-tip-convert-stdclass-object-to-multidimensional-array-and-convert-multidimensional-array-to-stdclass-object
PHP stdClass to Array and Array to stdClass – stdClass Object :: If Not True Then False
December 30, 2009 - <?php function arrayToObject($d) ... // Convert array to object and then object back to array $array = objectToArray($init); $object = arrayToObject($array); // Print objects and array print_r($init); echo "\n"; print_r($array); ...
🌐
IQCode
iqcode.com › code › php › convert-stdclass-object-to-array-php
convert stdclass object to array php Code Example
November 16, 2021 - $array = json_decode(json_encode($booking), true); ... $person = new stdClass(); $person-&gt;firstName = &quot;Taylor&quot;; $person-&gt;age = 32; //Convert Single-Dimention Object to array $personArray = (array) $person; //Convert Multi-Dimentional Object to Array $personArray = ...
Find elsewhere
🌐
ItSolutionstuff
itsolutionstuff.com › post › how-to-convert-object-into-array-in-phpexample.html
How to Convert Object into Array in PHP? - ItSolutionstuff.com
May 14, 2024 - Whenever you need to convert object to array in your php project then you can do that using (array). It is a pretty simple example to convert php object into array that way you can use array.
🌐
Edureka Community
edureka.co › home › community › categories › web development › php › convert stdclass object to array in php
Convert stdClass object to array in PHP | Edureka Community
June 26, 2022 - I fetched post_id from postmeta: $post_id = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE (meta_key ... => 142 ) How can I do this?
Top answer
1 of 8
254

You just add this code

$clasa = (object) array(
            'e1' => array('nume' => 'Nitu', 'prenume' => 'Andrei', 'sex' => 'm', 'varsta' => 23),
            'e2' => array('nume' => 'Nae', 'prenume' => 'Ionel', 'sex' => 'm', 'varsta' => 27),
            'e3' => array('nume' => 'Noman', 'prenume' => 'Alice', 'sex' => 'f', 'varsta' => 22),
            'e4' => array('nume' => 'Geangos', 'prenume' => 'Bogdan', 'sex' => 'm', 'varsta' => 23),
            'e5' => array('nume' => 'Vasile', 'prenume' => 'Mihai', 'sex' => 'm', 'varsta' => 25)
);

If you want to see is this stdClass object just call this

print_r($clasa);

If you want to convert an array to object code will be

$arr = array('a'=>'apple','b'=>'ball');
$arr = (object) $arr;

You don't need to use stdClass. It will automatically converted to stdClass

2 of 8
71

The quick and dirty way is using json_encode and json_decode which will turn the entire array (including sub elements) into an object.

$clasa = json_decode(json_encode($clasa)); //Turn it into an object

The same can be used to convert an object into an array. Simply add , true to json_decode to return an associated array:

$clasa = json_decode(json_encode($clasa), true); //Turn it into an array

An alternate way (without being dirty) is simply a recursive function:

function convertToObject($array) {
    $object = new stdClass();
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $value = convertToObject($value);
        }
        $object->$key = $value;
    }
    return $object;
}

or in full code:

<?php
    function convertToObject($array) {
        $object = new stdClass();
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $value = convertToObject($value);
            }
            $object->$key = $value;
        }
        return $object;
    }

    $clasa = array(
            'e1' => array('nume' => 'Nitu', 'prenume' => 'Andrei', 'sex' => 'm', 'varsta' => 23),
            'e2' => array('nume' => 'Nae', 'prenume' => 'Ionel', 'sex' => 'm', 'varsta' => 27),
            'e3' => array('nume' => 'Noman', 'prenume' => 'Alice', 'sex' => 'f', 'varsta' => 22),
            'e4' => array('nume' => 'Geangos', 'prenume' => 'Bogdan', 'sex' => 'm', 'varsta' => 23),
            'e5' => array('nume' => 'Vasile', 'prenume' => 'Mihai', 'sex' => 'm', 'varsta' => 25)
    );

    $obj = convertToObject($clasa);
    print_r($obj);
?>

which outputs (note that there's no arrays - only stdClass's):

stdClass Object
(
    [e1] => stdClass Object
        (
            [nume] => Nitu
            [prenume] => Andrei
            [sex] => m
            [varsta] => 23
        )

    [e2] => stdClass Object
        (
            [nume] => Nae
            [prenume] => Ionel
            [sex] => m
            [varsta] => 27
        )

    [e3] => stdClass Object
        (
            [nume] => Noman
            [prenume] => Alice
            [sex] => f
            [varsta] => 22
        )

    [e4] => stdClass Object
        (
            [nume] => Geangos
            [prenume] => Bogdan
            [sex] => m
            [varsta] => 23
        )

    [e5] => stdClass Object
        (
            [nume] => Vasile
            [prenume] => Mihai
            [sex] => m
            [varsta] => 25
        )

)

So you'd refer to it by $obj->e5->nume.

DEMO

🌐
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.
🌐
Danyal
danyal.dk › home › php: how to convert stdclass to array
PHP: How to convert stdclass to array - Full Stack Programmer
December 7, 2020 - $chairArray = object_to_array($chair); function objectToArray ($object) { if(!is_object($object) && !is_array($object)){ return $object; } return array_map('object_to_array', (array) $object); } ... I'm a skilled programmer specializing in ...
🌐
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 - Line 1: We create an empty stdClass object and assign values to its public properties: name, email, and status. Step 2: Use get_object_vars() to Convert the Object to an Array
🌐
PHPpot
phppot.com › php › php-object-to-array
PHP Object to Array Convert using JSON Decode - PHPpot
This is an alternate method to convert an object type into an array. The below program uses the same input object. It replaces the JSON encode decode via conversion with the typecasting statement. The output will be the same as we have seen above. The PHP typecasting syntax is shown below. It prepends the target data type enclosed with parenthesis. ... <?php $object = new StdClass(); $object->id = 5678; $object->name = "William"; $object->department = "CSE"; $object->destination = "Engineer"; print"<pre>"; print_r( (array) $object ); ?>
🌐
GitHub
gist.github.com › umidjons › 6349938
Read JSON string from file, convert PHP stdClass object to array · GitHub
Read JSON string from file, convert PHP stdClass object to array · Raw · json-file-convert-stdclass-object-to-array.php · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.