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 ) )
Best Method in the WORLD :)
if you use different methods you will have problems. This is the best method. You have ever seen.
one liner
Actually if you want to use this with multi-dimensional arrays you would want to use some recursion.
recursion is your friend:
In the simplest case, it's probably sufficient to "cast" the array as an object:
Another option would be to instantiate a standard class as a variable, and loop through your array while re-assigning the values:
As Edson Medina pointed out, a really clean solution is to use the built-in
json_
functions:This also (recursively) converts all of your sub arrays into objects, which you may or may not want. Unfortunately it has a 2-3x performance hit over the looping approach.
Warning! (thanks to Ultra for the comment):
I use the following to parse Yaml files associative arrays into an object state.
This checks all supplied arrays if there are objects hiding there, and turns them also in objects.
This turns a yaml configured as
to an array consisting of:
to an object of: