Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
301 lines (235 sloc) 6.92 KB


In PHP, an array is an ordered map that associates values to keys.

A new empty array can be defined like so:

$translations = [];

Or, it can be defined with initial values like so:

$translations = [
    'hello' => 'hola',
    'goodbye' => 'adios',

Note that the array’s content is encapsulated in square brackets.

Each element in the array (e.g. hello => hola) is made up of a key => value pair.

These key => value pairs are separated by the symbols =>, which is often verbalized as “points to” E.g. In describing this array, you might say: “hello points to hola”

Elements in an array are separated by commas; the comma after the last item in the array is optional.

In the above example, the keys of the array were Strings, and this is referred to as an associative array.

The keys of an array can also be Integers, and this is referred to as an indexed array:

$phrases = [
    0 => 'hola',
    1 => 'adios',
    2 => 'hasta luego',
    3 => 'por favor',
    4 => 'de nada'

Indexed arrays can also be written like this, where the key is implicitly defined, starting at 0.

$phrases = [
    'hasta luego',
    'por favor',
    'de nada',

It's not necessary for the elements of an array to be written on their own lines as shown in the above examples; the syntax can be compacted like so:

$phrases = ['hola', 'adios', 'hasta luego', 'por favor', 'de nada'];

Working with elements in an array

You can access elements in an array using the square bracket syntax, array[key].

echo $phrases[0]; # Output: hola
echo $translations['goodbye']; # Output: adios

You can add new values to an existing array using the square bracket syntax:

Example with an associative array:

$translations['good afternoon'] = 'buenas tardes';

This same approach can also be used to update an existing value in an array:

# Before
echo $translations['goodbye']; # adios

# Update
$translations['goodbye'] = 'adiós';

# After
echo $translations['goodbye']; # adiós

When adding an element to an indexed array, omitting the key will simply add the value as a new element at the end of the array.

$phrases[] = 'buenas tardes';

As an alternative to the square bracket syntax, elements in an array can be added/changed using the built-in functions array_push or array_replace.

Debugging arrays

The built-in PHP function var_dump can be used to output the contents of an array for debugging purposes.



array(2) { ["hello"]=> string(4) "hola" ["goodbye"]=> string(5) "adios" }

The output of var_dump is easier to read when it's nested in a HTML pre element.

echo '<pre>';
echo '</pre>';


array(2) {
  string(4) "hola"
  string(5) "adios"

To save you time, a function called dump is included in the helpers.php helper file discussed in notes on Imports.

Using this function is as simple as this:



array(2) {
  string(4) "hola"
  string(5) "adios"

Value types

While the key of an array can be either a String or an Integer, the values of an array can be many different data types, and the data types can be mixed.

$mixedBag = [
    False, # Booleans
    4.0, # Floats
    1, # Integers
    ['a', 'b', 'c'] # Even other Arrays!



array(4) {
  array(3) {
    string(1) "a"
    string(1) "b"
    string(1) "c"

An array containing other arrays is referred to as a multidimensional array.

$countries = [
    'US' => [
        'name' => 'United States',
        'languages' => ['English'],
    'CA' => [
        'name' => 'Canada',
        'languages' => ['English', 'French'],
    'MX' => [
        'name' => 'Mexico',
        'languages' => ['Spanish'],

Accessing elements in a multi-dimensional array requires multiple levels of square brackets

dump($countries['CA']['languages'][0]); # string(7) "English"

Iterating through arrays

PHP's built-in construct foreach is used to iterate through elements in an array.

Example 1

Print the name of all the countries in the array

foreach($countries as $countryCode => $country) {
    echo $country['name'].'<br>';


United States

Example 2

Print the country code (key) of all the countries in the array

foreach ($countries as $countryCode => $country) {
    echo $countryCode.'<br>';



Example 3

Print a line that uses the country name, code (key), and languages

foreach ($countries as $countryCode => $country) {
    echo '<br>Primary language(s) of '.$country['name'].' ('.$countryCode.'): <br>';

    foreach($country['languages'] as $key => $language) {
        echo $language.'<br>';


Primary language(s) of United States (US):

Primary language(s) of Canada (CA):

Primary language(s) of Mexico (MX):

Example 4

Update the array so that all the country names are uppercase

foreach ($countries as $countryCode => $country) {
    $countries[$countryCode]['name'] = strtoupper($countries[$countryCode]['name']);

Note how indexing is used in the foreach loop to access the current country in the iteration, $countries[$countryCode]

Alternatively, if you pass your value by reference, you can edit it directly, e.g.:

foreach ($countries as $countryCode => &$country) {
    $country['name'] = strtoupper($country['name']);

Built-in array functions

Skim the complete list of built-in PHP array functions so you're aware of what’s available.

Sampling of commonly used array functions:

  • count — Count all elements in an array
  • in_array — Checks if a value exists in an array
  • krsort — Sort an array by key in reverse order
  • ksort — Sort an array by key
  • rsort — Sort an array in reverse order
  • shuffle — Shuffle an array
  • sort — Sort an array
  • array_pop — Pop the element off the end of array
  • array_push — Push one or more elements onto the end of array
  • array_search — Searches the array for a given value and returns the first corresponding key if successful
  • array_shift — Shift an element off the beginning of array
  • array_slice — Extract a slice of the array
  • array_sum — Calculate the sum of values in an array