How do I split a string into a multidimensional array in PHP without loops?
My string is in the format "A,5|B,3|C,8"
![]() ![]() ![]() 1
|
|||
|
![]() ![]() |
Without loops at all? Can't be done. Without you having to write the loop? Explode or one of the regex "split" methods. |
||
|
![]() ![]() |
Without you actually doing the looping part, something based on
Will get you :
Of course, this portion of code could be re-written to not use a lambda-function, and work with PHP < 5.3 -- but not as fun ^^
|
||
|
![]() ![]() |
this returns an array like array('A' => 5, 'B' => 3 etc |
||
|
![]() ![]() |
You can use a combination of map and explode, but you're not really avoiding a loop, you're just not using for/while statements. |
||
|
![]() ![]() |
If your data is huge this could get heavy but here's one solution:
Would result into something like this:
|
||
|
![]() ![]() |
Depending on whether array_walk() counts as a loop...
|
||
|
![]() ![]() |
A small loop would be best.
|
||
|