开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜