...

View Full Version : variable as key in array



shadkeene
09-01-2009, 02:19 AM
Hi,
I've been trying to do this for a while, but to no avail. Its got to be possible, but not working. Here's my code:




$color = array("N" => "#FFFFFF", "L" => "#00FF00", "M" => "#FFFF00");



echo $zero_two;
echo $color["L"]."<br>";
echo $color['$zero_two'];

Why doesn't $color['$zero_two'] print out #00FF00 like $color["L"]? I've tried with '' and without around the $zero_two variable. Thanks for your time,
Shad

Fou-Lu
09-01-2009, 02:48 AM
'$zero_two' indicates a string, drop the quotations on it and $zero_two can be parsed as a variable. I assume that its been declared by this point.

shadkeene
09-01-2009, 02:54 AM
Thanks, I've tried without quotes and still not working. Here's where I declare the variable. It prints out fine by itself but not as a key/array combo.


$readfile=file("../../../data/zoa/tda.txt");
$issuetime=$readfile[2];
$first=split(":",$readfile[3]);
$zero_two = $first[1];
$second=split(":",$readfile[4]);
$two_four = $second[1];
$third=split(":",$readfile[5]);
$four_six= $third[1];
$fourth=split(":",$readfile[6]);
$six_twelve= $fourth[1];
$fifth=split(":",$readfile[7]);
$twelve_twofour= $fifth[1];

$color = array("N" => "#FFFFFF", "L" => "#00FF00", "M" => "#FFFF00");



echo $zero_two;
echo $color["L"]."<br>";
echo $color[$zero_two];

Fou-Lu
09-01-2009, 02:58 AM
Trim the value of $zero_two with a trim() first. Reading a line from a file always leaves the ending EOL on it.

shadkeene
09-01-2009, 03:04 AM
I would have never known! Thanks very much for your time and helpful advice,
Shad



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum