PHP array length

Home - Tutorials - Basics

Get php array length. In this tutorial I will show you how to determine the length and / or the size of a PHP array. You will learn the most important methods how to use PHP functions to get length, size and memory used by the array information.

Tutorial info:

Name:PHP array length
Total steps:1

Bookmark PHP array length

Step 1 - Determine PHP array length

PHP array length

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

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

  1. $colorList = array("apple"=>"red",
  2. "grass"=>"green",
  3. "sky"=>"blue",
  4. "night"=>"black",
  5. "wall"=>"white");
  7. 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.

Be sure to run the code on a reliable yet cheap web hosting so it won't drill a hole in your pocket.


Tags: php array length, array length, array size, php array size, php, array, length, size

PHP array length - Table of contents
Step 1 - Determine PHP array length

Follow phpf1 on Twitter

F1 Site Family
Java F1
JavaScript F1

Total time: 0.1455