An array is a collection of data in an indexed variable.

One of PHP’s strongest features is its strong support of arrays. While practically every language has in-order numerically-indexed arrays, PHP’s array is both easier to use, easier to nest, and allows for arbitrary data to be made an index. I know that’s a bit confusing. Suffice it to say that with arrays, you can store multiple values in a single variable.

= Example Arrays =

$fruits = array(‘apple’, ‘orange’, ‘pear’); /* output: Array ( [0] => apple [1] => orange [2] => pear ) */

$hybrids = array(‘Honda’ => array(‘Civic’, ‘Insight’), ‘Ford’ => array(‘Fusion’, ‘Escape’), ‘Toyota’ => array(‘Camry’, ‘Prius’)); /* output: Array ( [Honda] => Array ( [0] => Civic [1] => Insight )

[Ford] => Array
    (
        [0] => Fusion
        [1] => Escape
    )

[Toyota] => Array
    (
        [0] => Camry
        [1] => Prius
    )

) */

= Accessing values in an array = There are two primary ways to access the data in an array. The first and most direct way is to query it with a key: echo $fruits[1]; // Output: orange

You can also loop through each element with a foreach loop: $cars = array(‘Honda’ => ‘Insight’, ‘Toyota’ => ‘Camry’);

foreach ($cars as $key => $value) { echo “$key $value\n”; }

/* Output: Honda Insight Toyota Camry */