Created
August 2, 2012 06:46
-
-
Save imjjss/3234603 to your computer and use it in GitHub Desktop.
array to object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function arrayToObject($array) { | |
if(!is_array($array)) { | |
return $array; | |
} | |
$object = new stdClass(); | |
if (is_array($array) && count($array) > 0) { | |
foreach ($array as $name=>$value) { | |
$name = strtolower(trim($name)); | |
if (!empty($name)) { | |
$object->$name = arrayToObject($value); | |
} | |
} | |
return $object; | |
} | |
else { | |
return FALSE; | |
} | |
} | |
$person = array ( | |
'first' => array('name' => 'Richard') | |
); | |
$p = arrayToObject($person); | |
echo $p->first->name; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment