How can I obtain the number of keys whose values are not empty in an array?

Source: Internet
Author: User
What function is used to obtain the number of keys whose values are not empty in the array? For example, Array (& nbsp; [0] & nbsp ;=& gt; & nbsp; aa & nbsp; & nbsp; [1] & nbsp ;=& gt; & nbsp; bb & can you tell me how many functions are used to obtain the number of keys in an array that are not empty?
For example, array:

Array
(
[0] => aa
[1] => bb
[2] => cc
[3] =>
)
 
If count is used, the result is 4, and the key with no value is included. But do I want to get the number of keys whose values are not empty? It should be 3. is there any function that can be implemented?
------ Solution --------------------

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

------ Solution --------------------

Print_r (count (array_filter ($ arr )));
/*
Manual
Array array_filter (array $ input [, callback $ callback])
If the callback function is not provided, array_filter () deletes all entries in the input that are equal to FALSE.
The null Boolean value is false.
*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.