Page 1 of 1

String to array

Posted: Sun Dec 07, 2008 12:13 pm
by Citizen
I know a string like "a b c d" can be put into an array using explode...

But what about something a little more complicated?

"1=5,4=9,6=73,23=12, and so on"

How can we make that into and array that equals...

[1] = 5
[4] = 9
[6] = 73
[23] = 12

Re: String to array

Posted: Sun Dec 07, 2008 12:53 pm
by Mark Baker

Code: Select all

 
$testString = '1=5,4=9,6=73,23=12';
$testArray = array();
foreach(explode(',',$testString) as $testValue) {
    list($discard,$value) = explode('=',$testValue);
    $testArray[] = $value;
}
 

Re: String to array

Posted: Sun Dec 07, 2008 1:36 pm
by Citizen
Thanks!