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);