Home >Backend Development >PHP Tutorial >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.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
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.
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:
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.
Two methods define PHP array length or size count. Let’s see how these methods used to determine the length in the following examples.
Creating a simple array to count the elements.
Code:
<?php $flowers= ['Jasmine', 'Diasy', 'Rose']; echo "The count is: " . count($flowers); ?>
Explanation:
Output:
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:
Output:
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:
Output:
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:
Output:
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:
Output:
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:
Output:
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:
Output:
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.
The above is the detailed content of PHP array length. For more information, please follow other related articles on the PHP Chinese website!