/ Published in: PHP

Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php 'firstname' => 'Richard', 'lastname' => 'Castera' ); $p = (object) $person; echo $p->firstname; // Will print 'Richard' ?> <?php function arrayToObject($array) { return $array; } $object = new stdClass(); foreach ($array as $name=>$value) { $object->$name = arrayToObject($value); } } return $object; } else { return FALSE; } } ?> <?php ); $p = arrayToObject($person); ?> <?php // Now you can use $p like this: echo $p->first->name; // Will print 'Richard' ?>
URL: http://www.richardcastera.com/blog/php-convert-array-to-object-with-stdclass
Comments
