String to array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

String to array

Post 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
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: String to array

Post 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;
}
 
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Re: String to array

Post by Citizen »

Thanks!
Post Reply