2

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.

4 Answers 4

7

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])) {
  ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

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...

Comments

0
if (is_array($foo[0]));

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

Comments

0

By using $foo[0][1], you are actually accessing the first character of the string $foo[0].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.