Forum Moderators: coopster

Message Too Old, No Replies

string to AssociativeArray

How can I make an AssociativeArray from a string.

         

appi2

8:38 pm on Sep 19, 2006 (gmt 0)

10+ Year Member



If I have a string

$string = "puppies¦4¦fluffy kittens¦8¦tribbles¦7835687¦";

Whats the best way to get to

$AssociativeArray = array(puppies =>4 , fluffy kittens =>8 , tribbles =>7835687);

jezra

8:50 pm on Sep 19, 2006 (gmt 0)

10+ Year Member



This is untested, so your milage may vary. I would try something like this.

$string = "puppies¦4¦fluffy kittens¦8¦tribbles¦7835687¦";
$values = explode("¦",$string);
$my_array = array();
for ($i=0;$i<count($values)-1;$i+=2 )
{
$my_array["\"".$values[$i]."\""]=$values[$i+1];
}
print_r($my_array);
echo "damn, that's a lot of tribbles";