PHP Arrays – Indexed, Associative, Multidimensional

PHP Arrays – Indexed, Associative, Multidimensional

Arrays in PHP is a type of data structure that allows us to store multiple elements of similar data type under a single variable thereby saving us the effort of creating a different variable for every data.

The arrays are helpful to create a list of elements of similar types, which can be accessed using their index or key. Suppose we want to store five names and print them accordingly. This can be easily done by the use of five different string variables. But if instead of five, the number rises to a hundred, then it would be really difficult for the user or developer to create so many different variables. Here array comes into play and helps us to store every element within a single variable and also allows easy access using an index or a key. An array is created using an array() function in PHP.

You may like

An array stores multiple values in one single variable:


$cars = array("Volvo", "BMW", "Toyota");

echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";



I like Volvo, BMW and Toyota.


What is an Array?

An array is a data structure that stores one or more similar type of values in a single value. For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length.

An array is a special variable, which can hold more than one value at a time

If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:


$cars1 = "Volvo";

$cars2 = "BMW";

$cars3 = "Toyota";

However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?

The solution is to create an array!

An array can hold many values under a single name, and you can access the values by referring to an index number.


Create an Array in PHP

In PHP, the array() function is used to create an array:

There are three different kind of arrays and each array value is accessed using an ID c which is called array index.


There are 3 types of array in PHP.


  1. Indexed /Numeric array − An array with a numeric index. Values are stored and accessed in linear fashion.
  2. Associative array − An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.
  3. Multidimensional array − An array containing one or more arrays and values are accessed using multiple indices


Get The Length of an Array – The count() Function

The count() function is used to return the length (the number of elements) of an array:


$cars = array("Volvo", "BMW", "Toyota");

echo count($cars);




Complete PHP Array Reference

For a complete reference of all array functions, go to our complete PHP Array Reference.

The reference contains a brief description, and examples of use, for each function!

PHP Multidimensional array is used to store an array in contrast to constant values. Associative array stores the data in the form of key and value pairs where the key can be an integer or string. Multidimensional associative array is often used to store data in group relation.

Related: for PHP Arrays – Indexed, Associative, Multidimensional

You may like

Scroll to Top