Use of count() Function:
Syntax:
This function can take two arguments. The first argument is mandatory and the second argument is optional. It returns the total number of elements of the array that will be used in the first argument:
The first argument contains an array variable. The default value of the second argument is COUNT_NORMAL or 0 is used to count the length of a one-dimensional array. Another value of this argument is COUNT_RECURSIVE or 1 is used to count the length of the multi-dimensional array.
Example 1: Use of count() Function for One-Dimensional Array
Create a PHP file with the following script to count the length of a numeric array using the count() function. Here, an array of various data has been declared in the script. Next, the length of the array has been counted and printed:
Output:
The following output will appear after executing the script:
Example 2: Use of count() Function for Multi-Dimensional Array
Create a PHP file with the following script to count the length of a multi-dimensional array using the count() function. The array contains an array as an element in the multi-dimensional array. The optional argument of the count() function is required to count the elements of the inner arrays of the multi-dimensional array. The COUNT_RECURSIVE value has been used in the second argument value of the function to count the total length of the multi-dimensional array:
//Declare a multi-dimensional array
$multiArray = array(5, array('101','453','764'), 8, array('101','453'));
//Count total elements of the main array
$len = count($multiArray);
//Print the length of the main array
echo "Total elements of the main array is <b> $len. </b><br />";
//Count total elements of the array using COUNT_RECURSIVE
$len = count($multiArray, COUNT_RECURSIVE);
//Print the total elements of the main and inner array
echo "Total elements of the main and inner array is <b> $len. </b>";
?>
Output:
The following output will appear after executing the script. Here, the length of the main array is 4 that contains 2 numbers and 2 arrays. The length of the array with the elements of the inner array is 9 (4+3+2) because one inner array contains 3 elements and another inner array contains 2 elements:
Example 3: Use of count() Function for Associative Array
Create a PHP file with the following script to count the length of the associative array using the count() function. The multi-dimensional associative array has been used in the script. Like the previous example, the COUNT_RECURSIVE value has been used in the second argument of the count() function to calculate the length of the associative array with the elements of the inner array:
//Declare an associative array
$assoc_array = ['id' => '111896733',
'name' => 'Mir Abbas',
'marks' => [
'CSE305' => 79,
'CSE312' => 85,
'CSE401' => 75,
'CSE407' => 65]];
//Count total elements of the array using COUNT_RECURSIVE
$len = count($assoc_array, COUNT_RECURSIVE);
//Print the total elements of the array
echo "Total elements of the main and inner array is <b> $len. </b>";
?>
Output:
The following output will appear after executing the script. Here, the main array contains 3 elements, and the inner array contains 4 elements. Total elements are 3+4 or 7:
Use of sizeof() Function:
Another way to count the length of an array is to use the sizeof() function. It works similarly to the count() function. The syntax of this function is provided below:
Syntax:
This function can take two arguments. The first argument is mandatory and the second argument is optional. It returns the total number of elements of the array that will be used in the first argument:
Like the count() function, the first argument of this function contains an array variable. The second argument is optional, and the default value of this argument is 0 that is used to count the length of the one-dimensional array. Another value of this argument is 1 that is used to count the length of the multi-dimensional array.
Example 4: Use of sizeof() Function to Calculate the Length of the Array
Create a PHP file with the following script to count the length of a numeric and an associative array using the sizeof() function. The same array values of the previous examples have been used in this script. Here, the length of the arrays has been counted by using sizeof() function:
//Declare an array
$MyArr = array(10, 'John','Blue', 'Pizza', 3.45);
//Count the total number of elements of the array
$len = sizeof($MyArr);
//Print the length value
echo "Total elements of the numeric array is <b> $len. </b><br />";
//Declare an associative array
$assoc_array = ['id' => '111896733',
'name' => 'Mir Abbas',
'marks' => [
'CSE305' => 79,
'CSE312' => 85,
'CSE401' => 75,
'CSE407' => 65]];
//Count total elements of the array without optional argument
$len = sizeof($assoc_array);
//Print the total elements of the array
echo "Total elements of the main associative array is <b> $len. </b><br />";
//Count total elements of the array with optional argument
$len = sizeof($assoc_array, true);
//Print the total elements of the array
echo "Total elements of the main and inner associative array is <b> $len. </b>";
?>
Output:
The following output will appear after executing the script:
Conclusion:
In this article, two different ways to count the length of an array have been explained by using multiple examples. Now, PHP users can use either the count() function or the sizeof() function to count the length of the array. We hope you found the article helpful and please check out LinuxHint for more informative articles.