Total Number of Elements in Array - count()
PHP Array FunctionsThe count() function returns the number of elements in an array or the number of properties in an object.
Example:
$fruits = ['apple', 'banana', 'orange', 'mango'];
$total_fruits = count($fruits);
The count() function can be used with both indexed arrays and associative arrays.
Recursive Count
To count all the elements of a multidimensional array, you can set the optional mode parameter to COUNT_RECURSIVE.
Example:
$fruits = array( 'apples' => ['red' => 5, 'green' => 3], 'bananas' => 8);
$total_fruits = count($fruits, COUNT_RECURSIVE);
The resulting count includes all the elements in the array, including the values in nested arrays. In this case, there are a total of 4 elements in the $fruits array.