Zend PHP 7 Certification – Arrays – Enumerated Arrays

This post covers the Enumerated Arrays section of the Arrays chapter when studying for the Zend PHP 7 Certification.

An array in PHP is actually an ordered map, and the map is used as a way of ordering data by associating values to keys.

An array can be created using the array() language construct. It takes any number of comma-separated key => value pairs as arguments.

array(
    key  => value,
    key2 => value2,
    key3 => value3,
    ...
)

Enumerated arrays are arrays that can be indexed numerically, and are sometimes called indexed arrays.

As of PHP 5.4, it is more common to define an array using the square bracket syntax, which replaces array() with [].

  • $x = array(‘a’, ‘b’, ‘c’);
  • $x = [‘a’, ‘b’, ‘c’];
  • $x = array(0 => ‘a’, 1 => ‘b’, 2 => ‘c’);
  • $x = [0 => ‘a’, 1 => ‘b’, 2 => ‘c’];

The values can also be assigned manually.

$x[0] = 'a';
$x[1] = 'b';
$x[2] = 'c';

You’ll then be able to access each individual value by specifying the array variable, followed by the index in square brackets.

echo "Here are the letters " . $x[0] . ", " . $x[1] . " and " . $x[2] . ".";
// Outputs: Here are the letters a, b and c.

An array key can either be an integer or a string. Other types will be casted such as the below examples.

  • Strings containing valid integers will be cast to the integer type. For example, the key "8" will actually be stored under 8.
  • Floats are also cast to integers, which means that the fractional part will be truncated. For example, the key 8.7 will actually be stored under 8.
  • Bools are cast to integers too. For example, the key true will actually be stored under 1 and the key false under 0.

If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten. So in the following example, only the last key is used.

<?php
$array = array(
    1    => "a",
    "1"  => "b",
    1.5  => "c",
    true => "d",
);
var_dump($array); // Outputs: array (size=1) 1 => string 'd' (length=1)

The range() function creates an array containing a range of elements. Three parameters are involved, the start sequence, end sequence and step value.

By default, if no step value parameter is passed in, 1 is used.

<?php
$array = range(0, 9);
var_dump($array);

// Outputs:
array (size=10)
  0 => int 0
  1 => int 1
  2 => int 2
  3 => int 3
  4 => int 4
  5 => int 5
  6 => int 6
  7 => int 7
  8 => int 8
  9 => int 9
<?php
$array = range(0, 100, 10);
var_dump($array);

// Outputs:
array (size=11)
  0 => int 0
  1 => int 10
  2 => int 20
  3 => int 30
  4 => int 40
  5 => int 50
  6 => int 60
  7 => int 70
  8 => int 80
  9 => int 90
  10 => int 100

The range() function also works with letters.

<?php
$array = range('a', 'i');
var_dump($array);

// Outputs:
array (size=9)
  0 => string 'a' (length=1)
  1 => string 'b' (length=1)
  2 => string 'c' (length=1)
  3 => string 'd' (length=1)
  4 => string 'e' (length=1)
  5 => string 'f' (length=1)
  6 => string 'g' (length=1)
  7 => string 'h' (length=1)
  8 => string 'i' (length=1)

View the other sections:

Note: This article is based on PHP version 7.0.