How can i convert an array like this to object?
[128] => Array ( [status] => Figure A. Facebook's horizontal scrollbars showing up on a 1024x768 screen resolution. ) [129] => Array ( [status] => The other day at work, I had some spare time ) )
How can i convert an array like this to object?
[128] => Array ( [status] => Figure A. Facebook's horizontal scrollbars showing up on a 1024x768 screen resolution. ) [129] => Array ( [status] => The other day at work, I had some spare time ) )
Here are three ways:
Fake a real object:
Convert the array into an object by casting it to an object:
Manually convert the array into an object:
By using (array) and (object) as prefix, you can simply convert object array to standard array and vice-verse
There's no built-in method to do it as far as I'm aware, but it's as easy as a simple loop:
You can expound on that if you need it to build your object recursively.
you can simply use type casting to convert an array to object.
use this function that i've made:
Usage:
Little complicated but easy to extend technique:
Suppose you have an array
Suppose you have have a Person class which may have more or less attributes from this array. for example
If you still wanna change your array to the person object. You can use ArrayIterator Class.
Now you have iterator object.
Create a class extending FilterIterator Class; where you have to define the abstract method accept. Follow the example
The above impelmentation will bind the property only if it exists in the class.
Add one more method in the class PersonIterator
Make sure you have mutators defined in your class. Now you are ready to call these function where you want to create object.