Skip to content

Instantly share code, notes, and snippets.

@zorbash
Created March 28, 2012 21:53
Show Gist options
  • Save zorbash/2230896 to your computer and use it in GitHub Desktop.
Save zorbash/2230896 to your computer and use it in GitHub Desktop.
php object to array function to be used before json encoding of a class
function object_to_array($object, $utf_encode=FALSE)
{
if(is_array($object) || is_object($object))
{
$array = array();
foreach($object as $key => $value)
{
$value = object_to_array($value, $utf_encode);
if($utf_encode && is_string($value)){
$value = utf8_encode($value);
}
$array[$key] = $value;
}
return $array;
}
return $object;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment