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

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

WBOY
WBOYOriginal
2016-06-13 11:13:56943browse

请问获取数组中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 />

------解决方案--------------------
<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 />
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