PHP

PHP Array Length

PHP array length, count, and sizeof we can say they have different names but their functionality is almost the same.

PHP count or PHP sizeof function performs the same task of finding the exact length of an Array.

Let’s have some examples.

Example 1 – PHP count array

<?php

$phones=array("Samsung","Iphone","Oppo","Vivo");

echo count($phones);

?>

In the above example, we declare an array named “$phones” and stored some values in it (we can also push), then we printed the array values by using the PHP count method, in the parameter we put the array name “$phones”.

let’s check the output:

Output

4

As you can see it is showing the exact length of the array of $phones.

Let’s try the same example using sizeof method.

Example 2 – PHP sizeof array

<?php

$phones=array("Samsung","Iphone","Oppo","Vivo");

echo sizeof($phones);

?>

In the above example, we took the same array we just change the function.

In this example we are using the sizeof function, let’s check the output.

Output

4

So the sizeof function is also giving the same output as the count function.

Let’s try another example using a multidimensional array.

Example 3 – PHP multi-dimension array count

<?php

$phones=array(

      'phone'=>array('Samsung','Iphone','Oppo','Vivo'),
       'cars'=>array('Audi','Civic','Corolla','Alto')

);

echo sizeof($phones);

?>

In the above example, we declare two arrays named $phone and $cars and store them in a single array, and then we use the sizeof function for printing the correct array length let’s check out the results.

Output

2

This result is showing the array length that we have 2 arrays in $phones, if you want to find out the internal array value length so in count and sizeof function we can pass a second parameter value which can only be 1 or 0, 0 is a default value so we will pass 1 as you can see in the below code.

<?php

$phones=array(

       'phone'=>array('Samsung','Iphone','Oppo','Vivo'),
       'cars'=>array('Audi','Civic','Corolla','Alto')

);

echo sizeof($phones,1);

?>

Now by passing the second parameter value we can check the array length of an internal array in a multidimensional array.

Let’s check the results.

Output

10

As you can see in the output it is displaying the exact length of the multidimensional array.

If you want to see the length of a single array in a multidimensional array so you just have to pass the name of that array with the 1st parameter in the count or sizeof function, the code is below.

<?php

$phones=array(

       'phone'=>array('Samsung','Iphone','Oppo','Vivo'),
       'cars'=>array('Audi','Civic','Corolla','Alto')
);

echo sizeof($phones['phone'],1);

?>

Output

4

Now if you want to print your array or you want to know how many elements are in your array so you can check by using for loop let’s try the code below.

Example 4 – use count for print array by loop

<?php

$phone=array('Samsung','Iphone','Oppo','Vivo');

$leng=count($phone);

for($i=0; $i<$leng; $i++){

echo $phone[$i];

}

?>

Output

Samsung Iphone Oppo Vivo

Conclusion

In this tutorial, we discussed the PHP array length functions, and practice some examples using PHP count and PHP sizeof method.

Suggested Articles:

Leave a Comment