How to get Array length in PHP?

To get the total number of elements in an array, you may use the PHP count or sizeof functions. For example, this is how you may get the array length by count function:

$arr_nums = array (5,7,9,11,13);

echo "The size of array = ", count($arr_nums);

This will output the same as using the sizeof function:

$arr_nums = array (5,7,9,11,13);

echo "The size of array = ", sizeof($arr_nums);

Both functions return the size of an array (i.e. total number of elements) then which one to use? I will explain this, first, let me explain both these functions with examples.

PHP count function

The count function returns the size or number of elements in an array or something in an object. The count PHP function takes two parameters as shown in the syntax below:

int count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )

The $array_or_countable is the required parameter. You may specify the array name there for which you need to get the length.

The mode parameter is optional. The possible values for this parameter are 0 or 1. The 0 is the default value. The usage of value 1 is particularly useful for multidimensional arrays. It makes counting the array recursively.

The example of using count function

In the first example, I am using a one-dimensional array of mixed elements (numbers and strings). After array declaration, the count function is used to get the size of that array:

PHP code:

<?php


$arr_mix_item = array ('This',1,'is',2,'count',3,'function');

echo "The mixed array size = ", count($arr_mix_item);

?>

Output:

The mixed array size = 7

The example of using count with for loop

In this example, the count function is used with the for loop for iterating through the array elements. The purpose is to provide expression value in the for loop by count function to which value should loop execute.

In each iteration, the value is incremented by 1 and loop goes on till all elements of the array are displayed.

The code:

<?php

//Assigning values to an array

$arr_loop = array (11,22,33,44,55);



//Iterating through array

for ($i=0; $i <count($arr_loop); $i++){

echo "Current item: $arr_loop[$i] <br />";



}

?>

Output:

Current item: 11
Current item: 22
Current item: 33
Current item: 44
Current item: 55

Using sizeof function in for loop example

Now, let us use the sizeof function for getting the length of a PHP array. First, I will display the length of the array by using sizeof function.

This is followed by using sizeof in the for loop and displaying the array elements:

PHP code:

<?php

//Assigning values to an array

$arr_sizeof = array (25,50,75,100,125);



//Getting length of array

echo "Total elements in the array = ", sizeof($arr_sizeof), "<br /><br />";



//Iterating through array

for ($i=0; $i <sizeof($arr_sizeof); $i++){

echo "Current item: $arr_sizeof[$i] <br />";



}



?>

The output as you execute this code:

Total elements in the array = 5

 

Current item: 25

Current item: 50

Current item: 75

Current item: 100

Current item: 125

Using the mode parameter in count function example

In all above examples, we omitted to use the second parameter i.e. mode. So, count/sizeof functions used the default value which is 0.

As mentioned earlier, the value 1, or you may also use COUNT_RECURSIVE value instead of 1, will recursively count the array. This is useful for getting the size of multi-dimensional array as shown in the example below. To see the difference, I used both default and COUNT_RECURSIVE values and displayed the array length:

<?php

$sports = array('outdoor' => array('Baseball', 'Cricket', 'Football'),

              'indoor' => array('badminton', 'table tennis', 'volleyball'));



// Count with default value i.e 0

echo ("Count with default value = " .count($sports) . "<br /><br />");



// Count wiht 1 or recursive count

echo ("Count with 1/COUNT_RECURSIVE value = " .count($sports, COUNT_RECURSIVE));



?>

The output of this code is:

Count with default value = 2

 

Count with 1/COUNT_RECURSIVE value = 8

PHP count vs sizeof: What is the difference?

As per PHP official documentation, there is no difference between the count and sizeof functions. The sizeof function is just the alias of count function.

That means the sizeof uses the same as count function underneath.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!