2017-02-08 4 views
-2
$count = array(1 => 'one', 2 => 'two', 3 => 'three', 4=>''); 

配列には4つの値があり、1つのキーには値がありません。どのキーに値がある配列を数えるか?

キーに値がある配列の数を表示したいと思います。

+0

'array_filter()' –

+1

私は=> ' '3を推測している空ではないすべてのキーをフィルタリング'three'はタイプミスですか? – Tom

答えて

1

使用array_filter

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

、これを試してみて、その値

print_r(array_filter($count, create_function('$value', 'return $value !== "";'))); 
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 
関連する問題