$fruits = array("banana", "pineapple", array("apple", "mango"), "guava");
echo count($fruits,1);
The above code outputs 6, but I don't understand why. Can someone please explain it?
$fruits = array("banana", "pineapple", array("apple", "mango"), "guava");
Because is counting the array("apple","mango") as 1 element
count($fruits,1)// the second parameter will recursively count the array
+ 1 -> banana
+ 1 -> pineapple
+ 1 -> array("apple","mango")
+ 1 --------> apple
+ 1 --------> mango
+ 1 -> guava
____
6 elements
If you expected it to return 5, it's because the array on the 3rd position is counted as an element. If you expected it to return 4, count's second argument specifies whether it should count recursively or not.
count()can take a second parameter.