PHP stdClass to Array and Array to stdClass – stdClass Object - Comment Page: 3

I think every PHP coders have come accross Arrays and stdClass Objects (belongs to PHP Predefined Classes). Sometimes it's very useful convert Objects to Arrays and Arrays to Objects. This is easy if arrays and objects are one-dimensional, but might be little tricky if using multidimensional arrays and objects. This post defines two ultra simple recursive function to convert multidimensional Objects to Arrays and multidimensional Arrays to Objects. [inttf_post_ad1] Function to Convert stdClass Objects to Multidimensional Arrays <?php function objectToArray($d) { if (is_object($d)) { // Gets the properties of the given object ...

61 comments on “PHP stdClass to Array and Array to stdClass – stdClass Object - Comment Page: 3

1 2 3 4
    1. I’ve always just done this: $array = (array)$object;

      Reply
      • Hi Nigell,

        As I said at the beginning, this is easy if arrays and objects are one-dimensional. Your method doesn’t work with multidimensional arrays and objects.

        Try following:

        
        < ?php
        
                $init = new stdClass;
        
                // Add some test data
                $init->foo = "Test data";
                $init->bar = new stdClass;
                $init->bar->baaz = "Testing";
                $init->bar->fooz = new stdClass;
                $init->bar->fooz->baz = "Testing again";
                $init->foox = "Just test";
        
                print_r((array)$init);
        

        Output:

        Array
        (
            [foo] => Test data
            [bar] => stdClass Object
                (
                    [baaz] => Testing
                    [fooz] => stdClass Object
                        (
                            [baz] => Testing again
                        )
        
                )
        
            [foox] => Just test
        )
        

        First dimension is converted to array, but all others are still stdClass Objects, right?

        When you use objectToArray function output looks following:

        Array
        (
            [foo] => Test data
            [bar] => Array
                (
                    [baaz] => Testing
                    [fooz] => Array
                        (
                            [baz] => Testing again
                        )
        
                )
        
            [foox] => Just test
        )
        

        All stdClass Objects are converted to arrays.

        Reply
    2. Really appreciate this post! Helped me a lot! :)

      Reply
    3. Thank you so much for this post, you saved me countless hours!!!

      Reply
    4. Thanks…………. it’s save my time.

      Reply
    5. Thank you so must for posting this. You made my Sunday night coding not as bad as it could have been.

      Reply
    6. You don’t really need all this :-)
      Typecasting works!

      Try:

      $tmp = array("test" => "test");
      var_dump((object)$tmp);
      var_dump((array)(object)$tmp);

      Reply
      • Hi Tobi,

        Run following to see difference with typecasting and arrayToObject function:

        
        < ?php
        
                function arrayToObject($d) {
                        if (is_array($d)) {
                                /*
                                * Return array converted to object
                                * Using __FUNCTION__ (Magic constant)
                                * for recursive call
                                */
                                return (object) array_map(__FUNCTION__, $d);
                        }
                        else {
                                // Return object
                                return $d;
                        }
                }
        
                $tmp = array("test" => array("test" => "test","test2" => "test2"));
        
                var_dump((object)$tmp);
        
                var_dump(arrayToObject($tmp));
        

        Typecasting won’t work (unfortunately) with multidimensional arrays.

        Reply
    7. THANKSSSS! great job!!!

      If someone needs this method inside a static class change the line… return array_map(__FUNCTION__, $d); for this one return array_map([__CLASS__, __METHOD__], $d);

      Reply
    8. Hello Tobi,
      Thank you thanky you!!
      You helped me so much

      Reply
    9. no need for and of that.

      $a = array(‘key’ => ‘value’, ‘another key’ => ‘another value’);

      var_dump(json_decode(json_encode($a)));

      Reply
    10. Very nice functions; really helpful to work with AngularJS when sending POST data to PHP, since the way you receive it is as a stdClass object.

      Thanks a lot!

      Reply
    11. thanks, really good code.

      Reply
    12. hey. your first example code is i think not what it is intended. its both obj->array and array->obj.

      fil

      Reply
      • Hi fil,

        First it converts init (object) to array and then same array back to object and print init, array and object. Init and object stays exactly same, so easy to verify that the both functions works as expected and array is converted as it should be.

        Reply
    13. thanks for your post…

      Reply
1 2 3 4

Leave a Reply to Goga Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close