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

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 ...

64 comments on “PHP stdClass to Array and Array to stdClass – stdClass Object - Comment Page: 4

1 2 3 4
    1. Your post is very helpful, Thank’s! Could you please change a website’s font. I have broken my eyes

      Reply
      • hey dear,

        sorry but you can use casting in php it’s easier
        $object = (object) $array;
        // now $object has array elements as object;

        $array = (array) $object;
        // now $array has everything in $object as an array

        and thanks for listening :D

        Reply
        • Hi Alaa M. Jaddou,

          Try following:

          
          php -r '$object = (object) array("test" => array("testing" => array("moretesting"))); var_dump($object);'
          

          And

          
          php -r '$obj->foo->bar->fooz = "Testing"; $array = (array) $obj; var_dump($array);'
          

          Then you can see that casting is not working!? Just tried it on PHP 7.0.6.

          Reply
    2. A simpler way to do this would be:

      $myArray = json_decode( json_encode($myObject), true);

      The second argument for json_decode tells json_decode to use arrays. This has the added benefit of allowing for arrays in the objects.

      Reply
      • Note that doesn’t support binary data.

        Reply
    3. Thanks for sharing this article. It’s works for me and resolved my problem.

      Thanks

      Reply
    4. thanks helped me alot…

      Reply
    5. Best article,
      thanks for shearing…

      Reply
    6. Some difference with objects and arrays I found. It’s about 5 times more memory and CPU time to make possible avoid possible issues when order of properties(array keys) changed.

      Hydration is not strongly needed and use STD type intended to GET and SET futures with hidden options.

      I write framework on associative arrays and make higher performance without objects use when possible. I need only 0.5Mb for web-site with contents, comments and registered users to make possible all works.

      Most of developers forgot about objects and arrays in low lever are zeroes and ones and next simple strings. Seems, every property of object need one more cycle to found this property if defined(array need only one cycle to get all properties) – please correct me if !(bool)1.

      Also we can use null instead of false and use (bool)count(x) or !(bool)count(x) instead of count(x) === 0 to make it faster and better to understand.

      If we not use objects we more clever.

      Reply
1 2 3 4

Leave a Reply to Milch 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