PHP Arrays: An Introduction

An array holds a set of data items in a single variable. The data items in an array are referred to as elements. Each element has an associated key and a value. To create an array in PHP you can use the array() language construct as demonstrated here:

$ar = array('Morie', 'Miki', 'Coco', 'Halo');

You can also use the short syntax ([]) to create an array as of PHP version 5.4:

$ar = ['Morie', 'Miki', 'Coco', 'Halo'];

The two examples above are equivalent. Since they do not specify keys, PHP will use integer keys starting from zero. Array keys in PHP can be strings or integers, and a single array can include both string and integer keys, as we demonstrate here:

$ar = ['Morie', 'Miki', 'Coco', 'Halo', 'collie' => 'Mollie'];
print_r($ar);
/* print_r output (as seen in page source view):
Array
(
    [0] => Morie
    [1] => Miki
    [2] => Coco
    [3] => Halo
    [collie] => Mollie
) */

The syntax for adding elements with string keys is highlighted above: key and value are separated with =>.

The arrays demonstrated so far all consist of elements containing data of the same type: strings. However, arrays can hold data of different types:

$person = array(
    'name' => 'Jon',
    'age' => 26,
    'friends' => array('Matt', 'Kaci', 'Jess')
);
var_dump($person);
/* array(3) {
  ["name"]=>
  string(3) "Jon"
  ["age"]=>
  int(26)
  ["friends"]=>
  array(3) {
    [0]=>
    string(4) "Matt"
    [1]=>
    string(4) "Kaci"
    [2]=>
    string(4) "Jess"
  }
} */

How to Access and Modify PHP Array Elements

You can access the individual elements of an array using square bracket syntax. Here we use echo and square brackets to display the value of the 'age' element in the $person array defined above:

echo $person['age']; // 26

You can use square bracket syntax to change the value of an existing array element by assigning a new value using its key. We demonstrate here with another element from the $person array defined above:

$person['name'] = 'Jonathon';

You can also use square bracket syntax to add elements to an existing array. Here we define an array and then use square brackets with a string key to add a new element to it:

$ar = array('Morie', 'Miki', 'Coco', 'Halo');
// add new element to $ar
$ar['collie'] = 'Mollie';
print_r($ar);
/* Array
(
    [0] => Morie
    [1] => Miki
    [2] => Coco
    [3] => Halo
    [collie] => Mollie
) */

You can add elements to an array without specifying a key using empty square brackets. If you don't specify a key, PHP will use an integer one greater than the highest index value. We demonstrate that here using empty square brackets to add an element to the array defined in the example above:

$ar[] = 'Mittens';
print_r($ar);
/* Array
(
    [0] => Morie
    [1] => Miki
    [2] => Coco
    [3] => Halo
    [collie] => Mollie
    [4] => Mittens
) */

You don't have to use consecutive integers to assign values to new elements. You can choose any integer higher than currently used indexes:

$ar[7] = 'Winnie';
print_r($ar);
/* Array
(
    [0] => Morie
    [1] => Miki
    [2] => Coco
    [3] => Halo
    [collie] => Mollie
    [4] => Mittens
    [7] => Winnie
) */

is_array

You can use PHP's is_array function to determine whether data you are handling is an array:

if ( is_array($ar) ) {
    // code to handle array
}

count

You can use the PHP count function to find the length of an array. We use it here to determine the number of elements in the array used in the examples above:

echo count($ar); // 7

Back to top