I'm integrating an API to my website which works with data stored in objects while my code is written using arrays.
I'd like a quick and dirty function to convert an object to an array.
I'm integrating an API to my website which works with data stored in objects while my code is written using arrays.
I'd like a quick and dirty function to convert an object to an array.
First of all, if you need an array from object you probably should constitutes the data as array first. Think about it.
Don't use foreach statement or JSON transformations. If you're planning this, again you're working with a data structure, not with an object.
If you really need it use object orientated approach to have a clean and maintable code. For example:
Object as array
If you need all properties use transfer object
Short solution of @SpYk3HH
All other answers posted here are only working with public attributes. Here is one solution that works with javabean-like objects using reflection and getters:
From the first Google hit for "php object to assoc array" we have this:
Source at codesnippets.joyent.com.
You can also create function in PHP to convert object array.