Hướng dẫn php flatten associative array
I have an array that has keys and values. For eg:
I want to convert the keys from this into values, but I want the values to apear right after the keys. For eg:
I can easily write an iteration function that will do this... for eg:
...but I'm trying to find out if there's any way
this can be accomplished using Is there? asked May 30, 2009 at 7:08
aalaapaalaap 3,9675 gold badges49 silver badges57 bronze badges 2 Your own solution is probably the cleanest solution, so converting it to a "one-liner":
This avoids unnecessary and expensive array copies or sorting. Alternatively, lose the global:
answered May 30, 2009 at 8:58
Just JulesJust Jules 2912 silver badges8 bronze badges 2 I don't think this is possible - with the built-in functions you'll end up with all the keys then all the values:
You're going to have to use a loop like this:
answered May 30, 2009 at 7:16
GregGreg 310k53 gold badges365 silver badges328 bronze badges PHP 5.3+ version of Just Jules' answer, and a bit more readable:
answered Jun 6, 2014 at 9:23
coatesapcoatesap 10.3k5 gold badges22 silver badges33 bronze badges It is possible, but I don't think it is more readable or any faster. It would work with a less-known feature of PHP - the array addition:
answered May 30, 2009 at 7:59
soulmergesoulmerge 71.9k19 gold badges117 silver badges152 bronze badges 2 Could use an array_reduce to get close.
answered Jan 7, 2015 at 0:35
|