2016-04-30 78 views
0

我有一个数组,其中包含值字符串值和空值..我想计算所有赋予字符串值的元素。如何计算包含值的正确数组元素

 
    $items = array('name'=>'abc','contact'=>'0177....','address'=>'','country'='') 
    expected result: 
    $items = array('name'=>'abc','contact'=>'0177....') 
    and count new array element $items 

我想转换实际阵列 和计数新创建的数组

回答

0

需要简单地使用array_filter和存储它的一些变量中像作为

$items = array('name'=>'abc','contact'=>'0177','address'=>'','country'=>''); 
$filtered_arr = array_filter($items); 
echo count($filtered_arr); 
print_r($filtered_arr); 
0

这里需要使用array_filter()的元素。

echo count(array_filter($items)); 
+0

感谢所有, 这很简单但我不知道 –

+0

请投票答复并接受它,以防万一它解决您的目的。 :) –

0

试试这个

<?php 
$result = array(); 
$items = array('name'=>'abc','contact'=>'0177....','address'=>'','country'=>''); 
foreach($items as $key => $item) 
{ 
    if($item != '') 
     array_push($result, $item); 
} 
echo "Count ".count($result); 
?> 

OR

$items = array('name'=>'abc','contact'=>'0177','address'=>'','country'=>''); 
$result = array_filter($items); 
echo "Count".count($result); 
相关问题