How to get an array length in PHP

An array is a data structure to store multiple elements. Like any programming language, PHP provides helpful functions to work with arrays. We have seen many array functions in previous articles. 

One common array operation is to determine its length, which is the number of elements an array has. There are two similar functions to get array length in PHP.

In this article, we’ll help you learn these functions. First, we’ll see the syntax and then elaborate it through examples. So let’s start.

Array length in PHP: count function

Description

The count function counts all elements in an array.

Syntax

count(array $value, int $mode = COUNT_NORMAL): int

Arguments

  • $value  – An array
  • $mode  – An optional parameter with a default value of COUNT_NORMAL. We can pass COUNT_RECURSIVE (or 1) to count an array recursively. (Good for multi-dimensional arrays).

Return

  • Returns the number of elements in $value. 
  • If $value is not an array or object, the function returns 1. 
  • If $value null, the functions returns 0.

Examples

Let’s put this theory into action by examples.

<?php
//An array with many elements.
$arr = array("PHP","Javascript","Python","Java","C#","C++");
 
//Let's count this array using the count function and print it to console.
echo "Length of \$arr is: ".count($arr);
 
//OUTPUT
//Length of $arr is: 6
?>


Pretty straightforward. Remember, there’s a second parameter $mode for altering the function’s behavior. We’ll pass COUNT_RECURSIVE to it and see how it is different than the default COUNT_NORMAL.

<?php
//An array with many elements.
$arr = array("Name"=>"Robert","Age"=>"22","Skills"=>array("MongoDB","Express","React","Node.js"));
 
//Let's count this array using the count function (COUNT_NORMAL) and print it to console.
echo "Length of \$arr (Using COUNT_NORMAL) is: ".count($arr)."\n";
 
//Let's count this array using the count function (COUNT_RECURSIVE) and print it to console.
echo "Length of \$arr (Using COUNT_RECURSIVE) is: ".count($arr,COUNT_RECURSIVE);
 
//OUTPUT
//Length of $arr (Using COUNT_NORMAL) is: 3   
//Length of $arr (Using COUNT_RECURSIVE) is: 7
?>

The difference is clear, COUNT_NORMAL counts the elements in the first level, which are (Name, Age, Skills). COUNT_RECURSIVE counts the elements in the second level, which are the elements of Skills.

The count function can go beyond second-level, as the example shows.

<?php
//An array with many elements.
$arr = array(1,array(2,array(3,array(4))));
 
 
//Let's count this array using the count function (COUNT_RECURSIVE) and print it to console.
echo "Length of \$arr (Using COUNT_RECURSIVE) is: ".count($arr,COUNT_RECURSIVE);
 
//OUTPUT
//Length of $arr (Using COUNT_RECURSIVE) is: 7
?>


Cool. Let’s now move on to the next function: sizeOf.

Array length in PHP: sizeOf function

sizeOf function is just an alias of the count function.

Description

The sizeOf function counts all elements in an array.

Syntax

sizeOf(array $value, int $mode = COUNT_NORMAL): int

Arguments

  • $value  – An array
  • $mode  – An optional parameter with a default value of COUNT_NORMAL. We can pass COUNT_RECURSIVE (or 1) to count an array recursively. (Good for multi-dimensional arrays).

Return

  • Returns the number of elements in $value. 
  • If $value is not an array or object, the function returns 1. 
  • If $value null, the functions returns 0.

Examples

Let’s revisit the same example to get array length in PHP, using sizeOf instead.

<?php
//An array with many elements.
$arr = array("PHP","Javascript","Python","Java","C#","C++");
 
//Let's count this array using the count function and print it to console.
echo "Length of \$arr is: ".sizeOf($arr);
 
//OUTPUT
//Length of $arr is: 6
?>

sizeOf function does have the $mode parameter. Let’s alter the function to count an array recursively.

<?php
//An array with many elements.
$arr = array(1,array(2,array(3,array(4))));
 
 
//Let's count this array using the count function (COUNT_RECURSIVE) and print it to console.
echo "Length of \$arr (Using COUNT_RECURSIVE) is: ".sizeOf($arr,COUNT_RECURSIVE);
 
//OUTPUT
//Length of $arr (Using COUNT_RECURSIVE) is: 7
?>

The output is similar to the count function. You can use either of these functions to get array length in PHP.

BONUS: Common usage of array length in PHP

We do not leave a topic dangling by missing out on its practical use. Similarly, array length in PHP may have numerous uses, depends on the context you’re working. However, there is a frequently occurring practice you’ll encounter while coding.

The array length is mostly used in FOR loop to loop over an array. The idea is to terminate the loop once it reaches the end of an array and that’s where the array length in PHP factors in.

<?php
$arr = array("First","Second","Third","Fourth","Last");
 
// Loop over the array.
for($i = 0; $i < count($arr); $i++)
{
    echo "I am the ".$arr[$i]." element"."\n";
}
 
//OUTPUT
// I am the First element 
// I am the Second element
// I am the Third element 
// I am the Fourth element
// I am the Last element  
?>

See how the count helps the loop to terminate at the right time. It also helps to get array elements by index. That’s it, we hope you’ve enjoyed the article.

Want to explore further about PHP arrays?

We have many fun articles related to PHP arrays. You can explore these to learn more about arrays in PHP.

What is PHP isset function and when to use it

Convert array to string with PHP implode function

Make an associative array in PHP


Article Categories

Article Tags

    © 2022 Confident.Systems