Easy Object->Array Conversion in PHP

Are you ever working in a “mixed environment” where some things (like the Simple XML Element) return objects and some things like to work with arrays? If so, there’s a really easy way to convert all those objects into arrays without having to build some sort of ridiculous recursive function. Assuming you are using PHP 5.3 or newer, you can use the json_encode() and json_decode() functions to do the conversion for you. Here’s the simple one-liner to make that happen:

$your_array = json_decode(json_encode($your_object), TRUE);
This entry was posted in Geek Stuff. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>