4

I have an array. How can I get a list of the keys that have null values? Is there some short way to find them?

0

2 Answers 2

10

Actually, array_keys has an optional search_value parameter, so you can just put:

array_keys($array, null, true);

You must set the third parameter (strict comparison) to true for it to match only nulls.

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

1 Comment

Thanks! This is the kind of answer I was looking for. Very educational!
2

Here's the function that I came up with:

function find_nulls($a) {
    return array_keys(array_filter($a, function($b) {
       return is_null($b);
    }) );
}

It seems to work as desired.

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.