From: "Geoff Caplan" <[EMAIL PROTECTED]>

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?

How about is_array()?

if(is_array($foo['one']['two']))
{ echo 'multi dimensional array found'; }

You also have is_string(), empty(), etc...

---John Holmes...

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to