Skip to content

Instantly share code, notes, and snippets.

@arnorhs
Created September 28, 2017 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnorhs/38f8f00dc14cc39770eb19c7fea5c1bf to your computer and use it in GitHub Desktop.
Save arnorhs/38f8f00dc14cc39770eb19c7fea5c1bf to your computer and use it in GitHub Desktop.
horrible hack to recursively change custom objects into plain arrays
Class Yoyo
{
public static function objectToArray($obj) {
if (is_object($obj)) {
$reflect = new \ReflectionClass($obj);
$props = $reflect->getProperties();
$d = array();
foreach ($props as $prop) {
$prop->setAccessible(true);
$d[$prop->getName()] = $prop->getValue($obj);
}
$obj = $d;
}
return is_array($obj) ? array_map(function ($v) {
return static::objectToArray($v);
}, $obj) : $obj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment