Ask to get the value of the key in the array is not empty number, with what function

Source: Internet
Author: User
What is the function that gets the number of the key in the array that is not empty?
For example, arrays:

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 I want to get the number of keys that are not empty? Should be 3, what functions can be implemented?


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


$arr = Array (
0 = ' AA ',
1 = ' BB ',
2 = ' CC ',

);
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 () will delete all entries in input that are equal to FALSE
Null Boolean value is False
*/
  • Related Article

    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.