php sizeof, php count length of array, php array length count vs sizeof, php array,  array length php, How to calculate the length of an array in PHP

How to calculate the length of an array in PHP

In this article, we shall focus on the different methods and ways a PHP developer can use to calculate the length of an array. Calculating the length of an array is usually helpful and important when one wants to display the individual elements using the for loop

In the previous article, we discussed comprehensively how to work with arrays in PHP whereby we defined arrays as data structures in PHP that stores multiple values in a single variable, we also discussed the three types of arrays that includes, indexed, associative and multidimensional arrays and shown examples of each.

To calculate the length of an array in PHP, we use either one of the two inbuilt functions count function or sizeof function

  1. Get the length of an array using the count function

The syntax for count function is

<?php

count(array);

?>

The count function returns a single numerical value which is the result of the number of elements in an array. Elements in an array are separated using a comma (,).

Using count function in an indexed array

Suppose you have an index array

$computerbrands=array("HP","Samsung","Toshiba");

To calculate the size of this indexed array using the count function, use this code below

<?php

$computerbrands=array("HP","Samsung","Toshiba");

echo count($computerbrands);

?>

The result will be 3

Using count function in an associative array

To calculate the length of an associative array, for example,

$computerbrands=array("brand_1"=>"HP","brand_2"=>"Samsung","brand_3"=>"Toshiba");

Use the code below to get the length

<?php

$computerbrands=array("brand_1"=>"HP","brand_2"=>"Samsung","brand_3"=>"Toshiba");

echo count($computerbrands);

?>

The result of the above function will also be 3

Using count function in a multi-dimensional array

To calculate the length of a multi-dimensional array, like the one shown below,

$computerbrands = [

"brand_1" => [

"name" => "HP",

"ram" => "4 gb",

"hard disk" => "500 gb"

],

"brand_2" => [

"name" => "Samsung",

"ram" => "2 gb",

"hard disk" => "230 gb"

]

];

Use this code snippet

<?php

$computerbrands = [

"brand_1" => [

"name" => "HP",

"ram" => "4 gb",

"hard disk" => "500 gb"

],

"brand_2" => [

"name" => "Samsung",

"ram" => "2 gb",

"hard disk" => "230 gb"

]

];

echo count($computerbrands);

?>

The result will be 2

2. Get the length of an array using sizeof function

The second method you can use to calculate the length of an array in PHP is the sizeof function. The sizeof function works the same way as the count function and it also outputs a numerical value which is the length of that array

The below examples are used for each type of array

Using sizeof function in an indexed array

To calculate the size of this indexed array using the sizeof function, use this code below

<?php

$computerbrands=array("HP","Samsung","Toshiba");

echo sizeof($computerbrands);

?>

The result will be 3

Using sizeof function in an associative array

To calculate the length of an associative array use the code below

<?php

$computerbrands=array("brand_1"=>"HP","brand_2"=>"Samsung","brand_3"=>"Toshiba");

echo sizeof($computerbrands);

?>

The result of the above function will also be 3

Using sizeof function in a multi-dimensional array

To calculate the length of a multi-dimensional array using the size of function, like the one shown below,

Use this code snippet

<?php

$computerbrands = [

"brand_1" => [

"name" => "HP",

"ram" => "4 gb",

"hard disk" => "500 gb"

],

"brand_2" => [

"name" => "Samsung",

"ram" => "2 gb",

"hard disk" => "230 gb"

]

];

echo sizeof($computerbrands);

?>

The result will be 2

 

That’s how we calculate the length of an array in PHP using the count function and sizeof function, both work the same way and you can decide which function to use and you will achieve the same results