Created
September 28, 2017 13:32
-
-
Save arnorhs/38f8f00dc14cc39770eb19c7fea5c1bf to your computer and use it in GitHub Desktop.
horrible hack to recursively change custom objects into plain arrays
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
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