0

I noticed the following strange occurrence:

var_dump(in_array("test", array_keys(array("hello"))));

Yields: bool(true)

How is this possible? Array does not contains keys, therefore array_keys() will return an empty array. Test is not in this empty array, so why would it return true? Is this a bug in PHP?

1
  • 2
    array_keys(array("hello"))) is [0], it's not empty. Commented Aug 26, 2014 at 0:24

1 Answer 1

5

simple answer: loose typing, 'test' == 0

Use

var_dump(in_array("test", array_keys(array("hello")), true));

for strict typing

And all arrays have keys, if you don't explicitly assign a key, then PHP will assign one based on incrementing integers from 0 (hence 'test' == 0).... array_keys() will not be empty, because PHP will assign a key of 0 to your array entry of "hello"

Sign up to request clarification or add additional context in comments.

1 Comment

Actually forgot that arrays of course always have keys.. stupid. Thanks

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.