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

WBOY
Release: 2016-06-13 12:54:31
Original
1134 people have browsed it

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

Array
(
    [0] => aa
    [1] => bb
    [2] => cc
    [3] => 
)
 
如果用count,结果是4,把没有值的key也包含在内了。但我想得到值不为空的key的个数?应该是3,请问有什么函数可以实现吗?


------解决方案--------------------
<br />
$arr = array (<br />
		0 => 'aa',<br />
		1 => 'bb',<br />
		2 => 'cc',<br />
		3 => '' <br />
);<br />
function filter_empty($var) {<br />
	return ! empty ( $var );<br />
}<br />
echo count ( array_filter ( $arr, 'filter_empty' ) );<br />
Copy after login

------解决方案--------------------
<br />
print_r(count(array_filter($arr)));<br />
/*<br />
手册<br />
array array_filter ( array $input [, callback $callback ] )<br />
如果没有提供 callback 函数,array_filter() 将删除 input 中所有等值为 FALSE 的条目<br />
空的布尔值为false<br />
*/<br />
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template