Posted By

ki4ngel on 07/19/08


Tagged

php5


Versions (?)

PHP5 - function array2object()


 / Published in: PHP
 

URL: http://www.ab-d.fr/date/2008-07-19/

  1. // PHP5 only...
  2.  
  3. function array2object(array $array) {
  4. $object = new stdClass();
  5. foreach($array as $key => $value) {
  6. if(is_array($value)) {
  7. $object->$key = array2object($value);
  8. } else {
  9. $object->$key = $value;
  10. }
  11. }
  12. return $object;
  13. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: phifty on July 20, 2008

in php 5.2 or better, this would do the same thing, returning a stdClass object: jsondecode(jsonencode($somedeeparray));

Posted By: ki4ngel on July 20, 2008

very nice, I don't think. but this method is not recursive.

You need to login to post a comment.