Updated April 18, 2023
Introduction to PHP array length
PHP array length is defined as an array which is used to get many elements on them. Using the count () function and size of (), we could able to retrieve the count of an element. An array contains either string or integer values which can be either single or multi-dimensional. The array is used to hold a value in a key-value pair, an indexed array with many in-built functions for array processing. Using two Right functions (pre-defined) here saves a lot of time in calculating the values.
Syntax
PHP array length is defined as follows:
Count(array name, mode);
This function takes two parameters, namely array name followed by a mode that denotes the dimensions. An empty array denotes zero and returns ‘1’ for non-array values.
How does array length Work in PHP?
Array length is determined by the count and size of the function. As per the syntax, the optional mode argument is set to count() recursively, which will recursively count the number of elements in an array. This function is intensively used in Multi-dimensional array.
To know an array length, we have a few common reasons:
- Using a ‘for’ loop to move through the elements.
- No of the search elements returned.
- Calculating average values in an array.
But in PHP, to get the number of elements in an array, either sizeof or count function is enabled here to predict the array length in PHP. As the number of elements changes concerning user requirements in our code, it is very important to see the actual length of the array list. PHP has two in-built functions, namely count and size of.
Using count (): To count the elements.
We can use like this:
Code:
$a1=array(6,3,1,9);
echo " The size is given as =", count($a1);
So here, a count function returns the number of elements in an Object and simply counted in an associative array. In the above sample code, we have used a one-dimensional array. We have used PHPs native function count, so when we execute the above snippets, the output of the function is ‘4’. This is how we can get the values.
The second case is when we count the elements using parameter mode, to perform this, we have passed a constant ‘recursive’ as a parameter to count the elements. In this case, the length of an array is determined differently.
Code:
$avar = array (2,6,7, array (19,18,60));
$nelem = count ($avar, COUNT_RECURSIVE);
echo $nelem;
The above code displays the output as ‘7’ instead of the value ‘6’.
To perform iteration in array elements, we can use for-loop for iteration. The values should loop continues to execute. So, in each iteration step, the value gets incremented by 1. Care should be taken when using for loop in count () method as PHP lacks in differentiating indexed array and associative array. But most programmer developers pretend to use count () instead of sizeof() as it returns the memory size. Even though it is similar to the count () function, most of them stick to the count() function.
Examples of PHP array length
Two methods define PHP array length or size count. Let’s see how these methods used to determine the length in the following examples.
Example #1
Creating a simple array to count the elements.
Code:
<?php
$flowers= ['Jasmine', 'Diasy', 'Rose'];
echo "The count is: " . count($flowers);
?>
Explanation:
- When we execute the above code snippets, the output is shown as ‘3’ as the array elements have 3 elements.
- First, we had created an array of ‘flowers’, and in the next line, we used the count command.
Output:
Example #2
Code:
<?php
$program = [
'C++' => ['Polymorphism', 'Inheritance', 'Template'],
'Java' => ['Interface', 'Multithread', 'Exception'],
'PHP' => ['ArrayLength', 'Count']
];
echo "No. of count: ". count($program)."<br>";
echo "Multidimensional count: ". count ($program, 1);
?>
Explanation:
- Determines an array length with the count of ‘1’.
Output:
Example #3
Code:
<!DOCTYPE html>
<html>
<body>
<?php
$bike=array
(
"Hero Splender"=>array
(
"HP2345",
"HS3456"
),
"Royal Enfield"=>array
(
"R3",
"Tr5"
),
"Honda Activa 6G"=>array
(
"Classic 250"
)
);
echo "General count: " . sizeof($bike)."<br>";
echo "Recursive Number: " . sizeof($bike,1);
?>
</body>
</html>
Explanation:
- The above code determines the General count as ‘3’ and the array length of recursive mode to be ‘8’.
Output:
Example #4
Using For-loop.
Code:
<?php
$arr_iter = array (26,60,70,10,130,67);
echo "No of elements in the array = ", sizeof($arr_iter), "<br /><br />";
//Iterating through the array
for ($k=0; $k <sizeof($arr_iter); $k++){
echo "List of elements are: $arr_iter[$k] <br />";
}
?>
Explanation:
- The above code takes the length of an array using the sizeof function, and the array elements are iterated using for-loop.
- The output shows the list of values in an array in each iteration.
Output:
Example #5
Using Null value in mode.
Code:
<?php
$m[0] = 2;
$m[1] = 6;
$m[2] = 8;
value_res(count($m));
$n[3] = 1;
$n[4] = 3;
$n[8] = 5;
value_res(count($n));
value_res(count(null));
value_res(count(false));
?>
Explanation:
- The above code returns a parameter array as the value is assigned as null. So the output looks like this.
Output:
Example #6
Array length Using 2D array.
Code:
<?php
$foods = array('choclates' => array('Diary Milk', 'Cadbury Godiva', 'Nestle','Snikkers',
'Candy Craze'), 'Fast Food' => array('Nuggets', 'Salad Platters'));
echo count($foods, 1);
echo "</br>";
echo sizeof($foods, 1);
?>
Explanation:
- In the above code, we have used mode count as ‘1’; therefore, this multi-dimensional array counts the value as ‘9’.
Output:
Example #7
Word Count.
Code:
<?Php
$stringtype=' This is EDUCBA Asia largest Web Learning Platform providing courses in various Domains. We Provide Certification from many Leading Universities across the globe.';
$my1_array=explode(" ",$stringtype);
echo "No.Of words in the String = ".sizeof($my1_array);
?>
Explanation:
- The above program Stores a paragraph in a variable of type String.
- Here an array is created using explode function to split the array.
- Finally, count the number of words in a paragraph.
- We get the count as below.
Output:
Conclusion
Here we have seen how to determine the length or size of an array in PHP and also the various methods to show how PHP functions are used to take memory size used by the array. There is no difference between the count and size of the function. Depends upon the developer, the methods are picked while writing the code. In this article, we explored PHP’s array length with many examples, and also, we have also seen more about multi-dimensional arrays.
Recommended Articles
This is a guide to PHP array length. Here we discuss the introduction, syntax, and working of array length in PHP along with different examples and code implementations. You may also have a look at the following articles to learn more –