Re: Re[2]: [PHP] Odd behaviour of non-existent keys?
| From: | John Holmes | Date: | Wed, 25 Aug 2004 13:26:05 +0000 |
| Subject: | Re: Re[2]: [PHP] Odd behaviour of non-existent keys? | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-194677@lists.php.net to get a copy of this message | ||
From: "Geoff Caplan" <geoff@variosoft.com>
How about is_array()? if(is_array($foo['one']['two'])) { echo 'multi dimensional array found'; } You also have is_string(), empty(), etc... ---John Holmes...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?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'] ) ;