PHP Arrays
PHP Arrays
In PHP, an array is a special variable that can hold many values under a single name, and you can access the values by referring to an index number or a name.
Example
An array stores multiple values in one single variable:
$cars = array("Volvo", "BMW", "Toyota");
Try it Yourself »
PHP Array Types
In PHP, there are three types of arrays:
- Indexed arrays - Arrays with a numeric index
- Associative arrays - Arrays with named keys
- Multidimensional arrays - Arrays containing one or more arrays
PHP Array Items
Array items can be of any data type.
The most common are strings and numbers, but array items can also be objects, functions or even arrays.
You can have different data types in the same array.
Example
Array items of three different data types:
$myArr = array("Volvo", 15, ["apples", "bananas"]);
Try it Yourself »
PHP Array Functions
The real strength of PHP arrays are the built-in array functions.
Here we use the
count()
function to count the array items:
Example
Count items in the $cars array:
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
Try it Yourself »
For a complete reference of all array functions, go to our complete PHP Array Reference.