How to count all array elements in PHP ?
We have given an array containing some array elements and the task is to count all elements of an array arr using PHP. In order to do this task, we have the following methods in PHP:
Table of Content
Using count() Method
The count() method is used to count the current elements in an array. It returns 0 for an empty array.
Syntax
count($array, mode)
Example :
<?php
// PHP program to count all elements
// or values in an array
// Use of count() function
$array = array("Geek1", "Geek2",
"Geek3", "1", "2","3");
echo "Count first array elements: " . count($array) . "\n";
$array = array(
'names' => array("Geek1", "Geek2", "Geek3"),
'rank' => array('1', '2', '3')
);
echo "Recursive count: " . count($array, 1) . "\n";
echo "Normal count: " . count($array, 0);
?>
Output
Count first array elements: 6 Recursive count: 8 Normal count: 2
Using sizeof() Method
The sizeof() method is used to count the number of elements present in an array or any other countable object.
Syntax:
sizeof($array, mode)
Example :
<?php
// PHP program to count all elements
// or values in an array
// Use of sizeof() function
$array = array("Geek1", "Geek2",
"Geek3", "1", "2","3");
echo "Count second array elements: "
. sizeof($array) . "\n";
$array = array(
'names' => array("Geek1", "Geek2", "Geek3"),
'rank' => array('1', '2', '3')
);
echo("Recursive count: " . sizeof($array, 1) . "\n");
echo("Normal count: " . sizeof($array, 0) . "\n");
?>
Output
Count second array elements: 6 Recursive count: 8 Normal count: 2
Using a loop
To count all array elements in PHP using a loop, initialize a counter variable to zero. Iterate through the array elements using a loop and increment the counter for each element. The final count represents the total number of elements.
Example
<?php
// Single-dimensional array
$array = array("Geek1", "Geek2", "Geek3", "1", "2", "3");
$count = 0;
foreach ($array as $element) {
$count++;
}
echo "Count first array elements: $count \n";
// Multi-dimensional array
$array = array(
'names' => array("Geek1", "Geek2", "Geek3"),
'rank' => array('1', '2', '3')
);
// Recursive count
$count = 0;
foreach ($array as $subarray) {
foreach ($subarray as $element) {
$count++;
}
}
echo "Recursive count: $count \n";
// Normal count
echo "Normal count: " . count($array) . "\n";
?>
Output
Count first array elements: 6 Recursive count: 6 Normal count: 2
Using iterator_count with ArrayIterator
Using iterator_count with ArrayIterator involves wrapping the array in an ArrayIterator object, which provides a way to traverse the array. The iterator_count function then counts the elements of this iterator, giving the total number of elements in the array.
Example:
<?php
$array = [1, 2, 3, 4, 5];
$iterator = new ArrayIterator($array);
$count = iterator_count($iterator);
echo "Number of elements: $count";
?>
Output
Number of elements: 5
Using array_reduce() Function
The array_reduce() function can be used to count the elements of an array by iterating through each element and incrementing a counter.
Example:
<?php
function countElementsUsingReduce($array) {
return array_reduce($array, function($carry, $item) {
return $carry + 1;
}, 0);
}
// Example usage:
$arr = [1, 2, 3, 6, 3, 6, 1];
echo "The total number of elements is " . countElementsUsingReduce($arr);
?>
Output
The total number of elements is 7
Using array_walk Function
The array_walk function can be used to apply a callback function to each element of an array, and we can use this to count the elements by incrementing a counter inside the callback function.
Example
<?php
$arr = [1, 2, 3, 4, 5];
$count = 0;
array_walk($arr, function($item) use (&$count) {
$count++;
});
echo "Number of elements: " . $count;
?>
Output
Number of elements: 5
Using array_filter() Function
The array_filter() function filters elements of an array using a callback function. Although typically used to filter elements, it can also be utilized to count elements by counting the filtered array where all elements pass the filter (since no specific filter logic is applied).
Example: In this example, we'll use array_filter() to count the elements of an array by using a callback that returns true for all elements. This way, the filter will retain all elements, and we can then count the filtered array.
<?php
$array = [1, 2, 3, 4, 5];
$callback = function($element) {
return true;
};
$filteredArray = array_filter($array, $callback);
$count = count($filteredArray);
echo "The number of elements in the array is: $count";
?>
Output
The number of elements in the array is: 5