PHP explanation solution for count function

Source: Internet
Author: User
PHP's explanation of the count function
PHP Code
  
   $food = Array (' fruits ' = = Array (' Orange ', ' banana ', ' apple '),                            ' veggie ' = ' array (' carrot ', ' collard ', ' pea ')); c4/>//recursive Count              echo count ($food, count_recursive);//Output 8

How is the output 8? Not 6?

------Solution--------------------
Recursive cumulative Ah, to add a one-dimensional statistics 2,
------Solution--------------------
Count_recursive literally means recursive statistics ah ...

If The optional mode parameter is set to Count_recursive (or 1), count () would recursively count the array.

Follow the instructions in the manual,,, if your needs do not apply, self-write functions,
------Solution--------------------
What other people give you is not always your intention, often need to move their own hands.
Count ($food, count_recursive) returns the number of all nodes
And all you need is the number of leaf nodes.
PHP Code
$food = Array (' fruits ' = = Array (  ' orange ', ' banana ', ' apple '), ' veggie ' = ' = '  array (' carrot ', ' collard ', ' Pea ')); function Leay_count ($ar) {  $r = 0;  foreach ($ar as $item) {    if (Is_array ($item)) $r + = Leay_count ($item)    ; else $r + +;  }  return $r;} echo Leay_count ($food);
  • 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.