PHP Tutorial – 06 – Arrays
An array is used to store a collection of values in a single variable. Arrays in PHP consist of key-value pairs. The key can either be an integer (numeric array), a string (associative array) or a combination of both (mixed array). The value can be any data type.
Numeric arrays
Numeric arrays store each element in the array with a numeric index. An array is created using the array
constructor. This constructor takes a list of values which are assigned to elements of the array.
$a = array(1,2,3);
As of PHP 5.4 a shorter syntax is available, where the array constructor is replaced with square brackets.
$a = [1,2,3];
Once the array is created, its elements can be referenced by placing the index of the desired element in square brackets. Note that the index begins with zero.
$a[0] = 1; $a[1] = 2; $a[2] = 3;
The number of elements in the array is handled automatically. Adding a new element to the array is as easy as assigning a value to it.
$a[3] = 4;
The index can also be left out to add the value to the end of the array. This syntax will also construct a new array if the variable does not already contain one.
$a[] = 5; // $a[4]
To retrieve the value of an element in the array the index of that element is specified inside the square brackets.
echo "$a[0] $a[1] $a[2] $a[3]"; // "1 2 3 4"
Associative arrays
In associative arrays the key is a string instead of a numeric index, which gives the element a name instead of a number. When creating the array the double arrow operator (=>
) is used to tell which key refers to what value.
$b = array('one' => 'a', 'two' => 'b', 'three' => 'c');
Elements in associative arrays are referenced using the element names. They cannot be referenced with a numeric index.
$b['one'] = 'a'; $b['two'] = 'b'; $b['three'] = 'c'; echo $b['one'] . $b['two'] . $b['three']; // "abc"
The double arrow operator can also be used with numeric arrays to decide in which element a value will be placed.
$c = array(0 => 0, 1 => 1, 2 => 2);
Not all keys need to be specified. If a key is left unspecified the value will be assigned to the element following the largest previously used integer key.
$e = array(5 => 5, 6);
Mixed arrays
PHP makes no distinction between associative and numerical arrays, and so elements of each can be combined in the same array.
$d = array(0 => 1, 'foo' => 'bar');
Just be sure to access the elements with the same keys.
echo $d[0] . $d['foo']; // "1bar"
Multi-dimensional arrays
A multi-dimensional array is an array that contains other arrays. For example, a two-dimensional array can be constructed in the following way.
$a = array(array('00', '01'), array('10', '11'));
Once created the elements can be modified using two sets of square brackets.
$a[0][0] = '00'; $a[0][1] = '01'; $a[1][0] = '10'; $a[1][1] = '11';
They are also accessed in the same way.
echo $a[0][0] . $a[0][1] . $a[1][0] . $a[1][1];
The key can be given a string name to make it into a multi-dimensional associative array, also called a hash table.
$b = array('one' => array('00', '01')); echo $b['one'][0] . $b['one'][1]; // "0001"
Multi-dimensional arrays can have more than two dimensions by adding additional sets of square brackets.
$c[][][][] = "0000"; // four dimensions