Re[2]: [PHP] Odd behaviour of non-existent keys?
| From: | Geoff Caplan | Date: | Wed, 25 Aug 2004 13:04:51 +0000 |
| Subject: | Re[2]: [PHP] Odd behaviour of non-existent keys? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-194675@lists.php.net to get a copy of this message | ||
John,
>>
>> Why does the non-existent key in the array below evaluate to the
>> first char of the array value?
>>
>> $foo = '' ;
>> $foo['one']['two'] = 'test-value' ;
>>
>> // Evaluates to: string(1) "t"
>> var_dump( $foo['one']['two']['three'] ) ;
>>
JH> Strings are arrays. PHP probably takes the string 'three' and converts it to
JH> a integer to see what "key" of the string you're requesting.
JH> $foo = 'bar';
JH> echo $foo[1]; //should echo 'a'
JH> Does that help or do you need a deeper explanation (that I probably can't
JH> provide!) ;)
I think you are probably right - but this behaviour causes problems.
For example:
$foo['one']['two'] = "test-string" ;
// Evaluates to TRUE (not what's wanted!)
isset( $foo['one']['two']['three'] ) ;
I need a reliable way to test for the non-existence of a
multi-dimensional key. Any ideas? I guess I could convert the keys
into a string via a loop and compare that, but there must be a more
elegant way, surely?
------------------
Geoff Caplan