Posted By

gabrielsmith on 01/21/12


Tagged


Versions (?)

Array to Object


 / Published in: PHP
 

URL: http://www.richardcastera.com/blog/php-convert-array-to-object-with-stdclass

Converts multi-dimensional array to stdClass

  1. function arrayToObject($array) {
  2. if(!is_array($array)) {
  3. return $array;
  4. }
  5.  
  6. $object = new stdClass();
  7. if (is_array($array) && count($array) > 0) {
  8. foreach ($array as $name=>$value) {
  9. $name = strtolower(trim($name));
  10. if (!empty($name)) {
  11. $object->$name = arrayToObject($value);
  12. }
  13. }
  14. return $object;
  15. }
  16. else {
  17. return FALSE;
  18. }
  19. }

Report this snippet  

You need to login to post a comment.