Php PHPcode $ food = array ('Fruits '= & gt; array ('Orange', 'bana', 'apple '), 'veggi' = & gt; array ('php interpretation of the count function
PHP code
$food = array('fruits' => array('orange', 'banana', 'apple'), 'veggie' => array('carrot', 'collard', 'pea')); // recursive count echo count($food, COUNT_RECURSIVE); // output 8
How does one output 8? Isn't it 6?
------ Solution --------------------
Recursive accumulation. Add one-dimensional statistics 2,
------ Solution --------------------
COUNT_RECURSIVE literally refers to recursive statistics ......
If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count () will recursively count the array.
Follow the instructions in the manual. if your requirements are not applicable, write a UDF,
------ Solution --------------------
What others give you doesn't always mean to you. you often need to do it by yourself.
Count ($ food, COUNT_RECURSIVE) returns the number of all nodes
What 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);