Skip to content

Instantly share code, notes, and snippets.

@imjjss
Created August 2, 2012 06:46
Show Gist options
  • Save imjjss/3234603 to your computer and use it in GitHub Desktop.
Save imjjss/3234603 to your computer and use it in GitHub Desktop.
array to object
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