How many elements of array are not null or empty?
How do I count number of elements in an array that are not null?
For this array I should get 3
:
$array = 开发者_JS百科array();
$array[0] = 'foo';
$array[1] = '';
$array[2] = 'bar';
$array[3] = 'blabla';
How can I achieve this in PHP?
Use array_filter
[docs]:
count(array_filter($myarray))
$count = 0;
foreach ($array as $k=>$v) {
if (!empty($v)) {
$count++;
}
}
echo $count; // 3 with your array
精彩评论