Click to See Complete Forum and Search --> : split string into array
alxla
11-27-2007, 02:44 AM
Simply:
//I want to transform this:
$string = "{5;[aa]}{30;[ab]}{2;[bb]}";
//into this:
$array array(
"5"=>"aa",
"30"=>"ab",
"2"=>"b"
);
But how do I make the string to "exploade/split" like this?
Zipline
11-27-2007, 10:22 AM
I would do an explode using "}" as your value this would give you an array of values like "{5;[aa] then you can run a loop to remove the first "{" from each entry. Once that is done you can explode on the ";" and build your organized array, then loop the array and strip the "[" and "]" from each of the items.
jasonahoule
11-27-2007, 10:27 AM
That is a crazy looking string. You wouldn't want to simplify that? Perhaps
$string = "5:aa|30:ab|2:bb";
That way you just explode on "|" and then ":". There is no need for stripping anything.
alxla
11-27-2007, 11:30 AM
Yep, i thought it was crazy to. So I made it all into an array and used serialize().
Solved. :)