Search code examples
phpwarningsundefined-index

Undefined index - strange?


I experience something strange with an undefined index..

$vatcode = 'U25';
echo $this->vatcode_ids['U25']."\n";
echo $this->vatcode_ids[$vatcode]."\n";

foreach($this->vatcode_account_ids as $id => $vatcode){
    echo $vatcode."\n";
    echo $this->vatcode_ids[$vatcode]; // undefined index
}

this returns:

681
681
U25  

Notice: Undefined index: U25   in /var/www/.....php on line 64

I don't get it?!


Solution

  • From empty line printed before Notice massage i assume your $vatcode variable contains some ending new line character. If so it does not match any key in $this->vatcode_ids array.

    You should use some trimming function as Dan Lee suggested in comments.