views:

52

answers:

3

If I have this array:

$foo[0] = 'bar';
$foo[1] = 'bar bar';

echo $foo[0][1];

// result
a
// i.e the second letter of 'bar'

I want to check that $foo[0][1] is not set i.e if I had:

$foo[0][1] = 'bar';

it would evaluate to true, but in my original example of $foo[0] = 'bar' I would expect that:

isset($foo[0][1])

would return false;

What's the correct way to test that please.

A: 

doh

it's ->

array_key_exists($foo[0][1]);

I'm still confused as to why PHP thinks the $foo[0][1] is set though...

niggles
A: 
if (is_array($foo[0]));

and http://php.net/manual/en/language.types.string.php#language.types.string.substr for reference on returning "a";

Col. Shrapnel
+4  A: 

PHP doesn't have multidimensional arrays. It has arrays of arrays. It's important to understand the difference.

You need to do:

if (is_array($foo[0]) && isset($foo[0][1])) {
  ...
}
cletus