- Instant help with your Php coding problems

PHP array length

As the number of elements can change from time to time in your code it is important to get the actual length / size of the array. You have more options to get this information. PHP has 2 built-in functions and both returns with the number of elements in the array.

The first one is the count()  function that counts elements in an array. You can use it like this:

$colorList = array("apple"=>"red",
                   "grass"=>"green",
                   "sky"=>"blue",
                   "night"=>"black",
                   "wall"=>"white");
 
echo "Array size with count: ".count($colorList);

The second function is the sizeof() . However, sizeof() is only an alias of the count() so you can use it similar to count() the get the array length.

Share "PHP array length" with your friends