I have an array range $row['ADDwav'] - $row['ADDwav16']
How can I count the number of these that isset() and not NULL?
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(...))
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;
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));
array_filter w\out a callback will eliminate all that are equal to false, which is not the same as "not null".