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

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: 1

1 2 3 4
    1. Enjoying reading the posts here, thanks.

      Reply
    2. thanks very much..
      very useful for me , I have just started to learn OOP , and using objects is difficult for me initially ;)
      thanks again :D

      Reply
    3. Thanks a lot for your first function :)

      Reply
    4. Muitissimo obrigado.
      Cara tu não imagina a felicidade de eu conectar no meu servidor java aqui com PHP.

      Vlw =D

      Reply
    5. Thanks!

      I use this methods to data from json_decode function to get an array.

      Reply
      • set the 3. parameter of json_decode to true to archive the same.

        Reply
        • its the second parameter, sorry

          Reply
    6. very nice to get it

      thanx a tonn

      Reply
    7. Hi, in reference to what your using it for La5, you should just pass true as the second variable of json_decode to get it to return an associative array

      eg:

      $json ='{ “blah”:”bleh”}’;
      json_decode($json, TRUE);

      Reply
      • Thanks pretty kool, saved a lot of time :)

        Reply
    8. Thank you, it’s clean, documented, and working!

      Reply
    9. Nice! This is quite handy.

      Reply
    10. Thanks for the input. yes, it works.I give you ten

      Reply
    11. Very rarely am I able to copy and paste a block of code and have it work. WOW! You hit the nail on the head and helped me after a very frustrating day.

      Thanks

      Reply
    12. You saved my day. This code it directly to the point.
      Really good work.

      Reply
    13. Congratulations. Simple and efficient.

      Reply
1 2 3 4

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