What are Arrays?
Introduction to Arrays in PHP
In previous tutorials we explained the concept of variables, which are named memory locations used to store a single value or piece of data. In a way arrays are similar to variables, but instead of just storing an individual piece of data, they can store groups of related data. A list of e-mail addresses, names of employees, or a list of products, are just some examples of data that can be stored in an array. Essentially any data that can be grouped together can be stored in an array. Once your understand the concept of arrays, it will become impossible to program without them.
An array can consist of any number of elements, or in other words data, were each element is distinguished and referenced by a unique identifier called a key. Array elements by default have numerical keys which are indexed sequentially, but they can also be indexed by user defined keys. An array that is indexed by numbers is called a numerically indexed array while an array that is indexed by strings is called an associative index array.
Numerically Indexed Arrays
In a numerically indexed array each element is indexed sequentially by an integer starting from zero by default, were the key or index relates to the element’s position within the array. Below is a simple illustration of an array. Note, the resemblance between the arrangement of an array and a table. The first column is the unique index of the array element and the second column is the actual data value. Each subsequent row follows the same pattern.

Let’s create this array in PHP:
<?php $ice_cream = array(0 => 'vanilla', 1 => 'chocolate', 2 => 'strawberry'); //creating an array called $ice_cream and creating three elements ?>
One method of creating an array is using the built-in PHP Array() construct, which is used to create mutilple elements at the same time. The array() construct accepts key => value pairs delimited by commas and returns an array with the supplied parameters. In this example we defined an array variable called $ice_cream that contains three elements ‘vanilla’, ‘chocolate’, and ’strawberry’ with the corresponding numerical keys. Note, in this example we have explicitly specified the indices or keys for the elements. If we didn’t assign the keys, PHP will automatically index the elements numerically starting at zero by default.
To access the individual elements of an array, we reference the index of the element in brackets after the array identifier. Lets look at an example:
echo $ice_cream[0]; //prints vanilla echo $ice_cream[1]; //prints chocolate echo $ice_cream[2]; //prints strawberry
In the example above, the first echo statement simply prints out the first element from our $ice_cream array (keep in mind that array keys start at zero by default, so the first element in a numerically indexed array is referenced like this: $array[0], unless you explicitly specified a different key). Lets say that we wanted to add more flavors to our $ice_cream array, we can store additional elements by simply using the following assignment statement:
$ice_cream[3] = 'butter pecan'; $ice_cream[4] = 'pistachio'; $ice_cream[] = 'coffee';
In this example each subsequent line adds another element to our array. Notice in the last assignment statement we left the brackets empty; PHP keeps track of the last element position and in this situation PHP will automatically index subsequent elements with the next consecutive key. In this case the element 'coffee' is assigned the element key 5.
Similar to a variable, we can dynamically change the value of an array element anytime within our script. In the following example we’ll change the value of the forth element:
$ice_cream[3] = 'Neapolitan'; print_r($ice_cream); //prints all the elements in an array
The output:
Array (
[0] => vanilla
[1] => chocolate
[2] => strawberry
[3] => Neapolitan
[4] => pistachio
[5] => coffee
)
The print_r() function is used to display the contents of an array, in a more readable format. Notice the forth element $ice_cream[3] has been changed to Neapolitan.
Associative Arrays
As the name implies, associative arrays allow us to use keys that have a better relationship to the value. We can use meaningful strings instead of integers to index elements in an associative array. Numerically indexed arrays are good for storing data as a sequential list, while on the other hand associative arrays are a logical choice for data that can be stored representatively.
In the following example we have created an array called $team_members, to store all the names of members working on a fictional project. We’ll use their names as values and their corresponding roles as keys.
//creating arrays using the Array() construct $team_members = array( 'project lead' => 'John Lead', 'programmer' => 'Joe Bytes', 'designer' => 'Michelle Art', 'animation' => 'Tom Draws', 'sound' => 'Jim Play' ); //creating array elements individually $team_members['project lead'] = 'John Lead'; $team_members['programmer'] = 'Joe Bytes'; $team_members['designer'] = 'Michelle Art'; $team_members['animation'] = 'Tom Draws'; $team_members['sound'] = 'Jim Play';
Just like numerical arrays we can create and initialize multiple elements at once with the built-in Array() construct. We can also create array elements individually. Accessing associative array elements is also similar:
echo $team_members['programmer']; //displays Joe Bytes
In this tutorial we covered the fundamentals of creating and storing data within an array. Now that we have a mechanism for storing a group of data, we can readily traverse it and perform a number of different functions such as sorting, looping, and manipulating the data. We’ll cover these topics in the second part of this tutorial.
