0

Just something i cant understand why it does this or if i am getting something wrong.

I am trying to check if an array key == a sertain string in a foreach loop. this is what i have (only really simple, but... bugging me...)

foreach($option_arr as $key => $value){
    echo $key; // this is just a test
    if($key == "'Featured'"){
        $featuredCheckCount++;
    }
    if($key == "'Home'"){
        $homeCheckCount++;
    }
}

The output for the echo $key is 'Featured' WITH the quotes. this is of course OK, but i would have expected the result to be without the quotes. it just makes the check for is == look messy by having to write it with the quotes.

if($key == "'Featured'") // with the quotes.

This is not really important, but i do like to understand why at time, rather than just accepting this.

I cant find an explanation on google, so any explanation would be welcomed. or a better way of doing this if i should be using something different.

Ford
  • 537
  • 6
  • 20
  • 1
    Please show the array containing this data – baao Jan 30 '15 at 20:15
  • 2
    It seems that the single quotes are part of the key.. How is $option_arr generated? What do you get when you var_dump($option_arr);? – khartnett Jan 30 '15 at 20:17
  • 2
    Can you fix `$option_arr`? The explanation lies in how the array is being built to begin with. – Mike Brant Jan 30 '15 at 20:19
  • @khartnett ahh well spotted silly mistake... i have an inline if statement and out of habit have escaped the quotes, as name="ID['.$row->ID.'][\'Featured\']"... thanks and i must need sleep – Ford Jan 30 '15 at 20:37
  • @Ford Since it only was a typo, i think you can delete your question, since it doesn't serve anyone – Rizier123 Feb 17 '15 at 19:15

0 Answers0