Djacksway
Djacksway

Reputation: 457

Find all keys in an array that are not equal to zero

How can I setup the function array_keys to return all keys in my array that have values that are not equal to zero.

Upvotes: 0

Views: 1612

Answers (1)

user142162
user142162

Reputation:

You can do this by filtering the array first:

$results = array('ab' => 0, 'ba' => 53, 'pl' => 23, 'ct' => 0);

$non_zero = array_keys(array_filter($results, function($item)
{
    return $item !== 0;
}));

// Value of $non_zero:
//   array('ba', 'pl')

Upvotes: 4

Related Questions