Home>Article>Backend Development> 请教获取数组中key的值不为空的个数,用什么函数

请教获取数组中key的值不为空的个数,用什么函数

WBOY
WBOY Original
2016-06-13 12:54:31 1077browse

请问获取数组中key的值不为空的个数,用什么函数?
例如数组:

Array
(
[0] => aa
[1] => bb
[2] => cc
[3] =>
)

如果用count,结果是4,把没有值的key也包含在内了。但我想得到值不为空的key的个数?应该是3,请问有什么函数可以实现吗?


------解决方案--------------------

$arr = array (
0 => 'aa',
1 => 'bb',
2 => 'cc',
3 => ''
);
function filter_empty($var) {
return ! empty ( $var );
}
echo count ( array_filter ( $arr, 'filter_empty' ) );

------解决方案--------------------

print_r(count(array_filter($arr)));
/*
手册
array array_filter ( array $input [, callback $callback ] )
如果没有提供 callback 函数,array_filter() 将删除 input 中所有等值为 FALSE 的条目
空的布尔值为false
*/
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn