PHP Indexed Arrays
There are two ways to create indexed arrays:
The index can be assigned automatically [index always starts at 0], like this:
$cars = array["Volvo", "BMW", "Toyota"];
or the index can be assigned manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
The following example creates an indexed array named $cars, assigns three elements to it, and then prints a text containing the array values:
Example
Try it Yourself »
Loop Through an Indexed Array
To loop through and print all the values of an indexed array, you could use a for
loop, like this:
Example
Try it Yourself »
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 Exercises
An indexed array is a simple array in which data elements are stored against numeric indexes. All the array elements are represented by an index which is a numeric value starting from 0
for the first array element.
Creating an Indexed Array
There are two different ways of creating an indexed array in PHP, they are,
Syntax for the 1st way to create indexed array:
and the syntax for the 2nd way to create indexed array is:
No matter how we initialize the array, we can access the array as follows,
Accessing the 2nd array... Swift Brezza Ertiga Accessing the 1st array... Huracan Urus Aventador
Hence, to access an indexed array, we have to use the array name along with the index of the element in square brackets.
Traversing PHP Indexed Array
Traversing an array means to iterate it starting from the first index till the last element of the array.
We can traverse an indexed array either using a for
loop or foreach
. To know the syntax and basic usage of for
and foreach
loop, you can refer to the PHP for and foreach loop tutorial.
Using for
loop
While using the for
loop to traverse an indexed array we must know the size/length of the
array, which can be found using the count[]
function.
Following is the syntax for traversing an array using the for
loop.
Using the foreach
loop
using foreach
to traverse an indexed array is a better approach if you have to traverse the whole array, as we do not have to bother about calculating the size of the array to loop around the array.
Below we have used the foreach
to traverse the $lamborghinis
array.
Advantages of Indexed Array
Here are a few advantages of using indexed array in our program/script:
- Numeric index values make it easier to use and having numeric indexes are common to almost all the programming languages, hence it also makes the code more readable for others who go through your code.
In this tutorial you'll learn how to store multiple values in a single variable in PHP.
What is PHP Arrays
Arrays are complex variables that allow us to store more than one value or a group of values under a single variable name. Let's suppose you want to store colors in your PHP script. Storing the colors one by one in a variable could look something like this:
But what, if you want to store the states or city names of a country in variables and this time this not just three may be hundred. It is quite hard, boring, and bad idea to store each city name in a separate variable. And here array comes into play.
Types of Arrays in PHP
There are three types of arrays that you can create. These are:
- Indexed array — An array with a numeric key.
- Associative array — An array where each key has its own specific value.
- Multidimensional array — An array containing one or more arrays within itself.
Indexed Arrays
An indexed or numeric array stores each array element with a numeric index. The following examples shows two ways of creating an indexed array, the easiest way is:
Note: In an indexed or numeric array, the indexes are automatically assigned and start with 0, and the values can be any data type.
This is equivalent to the following example, in which indexes are assigned manually:
Associative Arrays
In an associative array, the keys assigned to values can be arbitrary and user defined strings. In the following example the array uses keys instead of index numbers:
The following example is equivalent to the previous example, but shows a different way of creating associative arrays:
Multidimensional Arrays
The multidimensional array is an array in which each element can also be an array and each element in the sub-array can be an array or further contain array within itself and so on. An example of a multidimensional array will look something like this: