Created
June 30, 2013 21:13
-
-
Save pippinsplugins/5896919 to your computer and use it in GitHub Desktop.
Simple PHP object to array conversion.
This file contains hidden or 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
If $foo is an object such as: | |
` | |
stdClass Object | |
( | |
[something] => 279 | |
[something_2] => 1 | |
) | |
` | |
You can convert it to an array by simply doing: | |
$foo = (array) $foo; | |
Mind blown. |
The reverse works too. Convert array to object $foo = (object) $foo;
Casting FTW!
If you have nested objects however, it may not always convert all of the child items to Arrays. For that. I simply just do the following:
$array = json_decode(json_encode($object));
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You're adorable.