Skip to content

Instantly share code, notes, and snippets.

@armetiz
Created March 13, 2012 10:29
Show Gist options
  • Save armetiz/2028036 to your computer and use it in GitHub Desktop.
Save armetiz/2028036 to your computer and use it in GitHub Desktop.
PHP - Object to Array
public function normalize($object, $format = null)
{
$reflectionObject = new \ReflectionObject($object);
$reflectionProperties = $reflectionObject->getProperties(\ReflectionMethod::IS_PUBLIC);
$attributes = array();
foreach ($reflectionProperties as $property) {
$attributeName = $property->getName();
$attributeValue = $property->getValue ($object);
if (null !== $attributeValue && !is_scalar($attributeValue)) {
$attributeValue = $this->serializer->normalize($attributeValue, $format);
}
$attributes[$attributeName] = $attributeValue;
}
return $attributes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment