Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    May 2007
    Posts
    162
    Thanks
    13
    Thanked 0 Times in 0 Posts

    variable as key in array

    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:



    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    '$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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder
    Join Date
    May 2007
    Posts
    162
    Thanks
    13
    Thanked 0 Times in 0 Posts
    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.

    Code:
    $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];

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Trim the value of $zero_two with a trim() first. Reading a line from a file always leaves the ending EOL on it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Regular Coder
    Join Date
    May 2007
    Posts
    162
    Thanks
    13
    Thanked 0 Times in 0 Posts
    I would have never known! Thanks very much for your time and helpful advice,
    Shad


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •