JohnK
JohnK

Reputation: 7337

How to get list of array keys with null values?

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?

Upvotes: 4

Views: 3232

Answers (2)

Don't Panic
Don't Panic

Reputation: 41810

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.

Upvotes: 10

JohnK
JohnK

Reputation: 7337

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.

Upvotes: 2

Related Questions