Array to Object


/ Published in: PHP
Save to your folder(s)

Converts multi-dimensional array to stdClass


Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.