0

I have an array range $row['ADDwav'] - $row['ADDwav16']

How can I count the number of these that isset() and not NULL?

0

4 Answers 4

3

You can use array_filter with is_null (as callback) for that:

count(array_filter($row, "is_null"))

If you want the ones that are not null, then you'd have to subtract that from the original array length still count($row) - count(array_filter(...))

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

Comments

1

Try something like that

$counter = 0;

foreach($row as $r)
{
    if($r !== null)
    {
        $counter++;
    }
}

echo "Total not null items : " . $counter;

The above code will work. Also the values into foreach are all set, otherwhise will not loop through them ;)

In case your array is like that :

$row = array(
    'ADDwav' => null,
    'ADDwav1' => 'somevalue',
    'ADDwav2' => 'anothervalue',
    'ADDwav3' => '',
    'ADDwav...' => '...',
    'ADDwav16' => null
);

and you like to count the values that are not empty or null then you have to modify the code in the next form :

$counter = 0;

foreach($row as $k => $r)
{
    if($r !== null || $r !== '')
    {
        $counter++;
    }
}

echo "Total not null items : " . $counter;

Comments

0

use array_filter and is_null

   foreach($row as $r)

      if($r !== null)
...
..
.

Comments

0

This one will remove the array which is false or null...

            $entry = array(
               0 => 'foo',
               1 => false,
               2 => -1,
               3 => null,
               4 => ''
              );

            echo  count(array_filter($entry));

1 Comment

array_filter w\out a callback will eliminate all that are equal to false, which is not the same as "not null".

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.