如何计算数组中哪些键存在值?

时间:2017-02-08 10:19:15

标签: php

 $count = array(1 => 'one', 2 => 'two', 3 => 'three', 4=>'');

数组有四个值,一个键没有值。

我想显示哪个键有值的数组的数量。

3 个答案:

答案 0 :(得分:1)

试试这个,

$array = array(1 => 'one', 2 => 'two', 3 => 'three', 4=>'');
$count = count(array_filter($array));
echo $count;

答案 1 :(得分:0)

使用array_filter并过滤掉所有值为空的键

print_r(array_filter($count, create_function('$value', 'return $value !== "";')));

答案 2 :(得分:0)

//Use functions array_filter() and count()    
$fullarray = array(1 => 'one', 2 => 'two', 3 => 'three', 4=>'');
$filteredArray = array_filter($fullarray);//filters the values of an array using a callback function
$count=count($filteredArray);//returns the number of elements in an array.
print_r($count);//display count
相关问题