Arrays in PHP
What is an array?
Put simply, an array is a collection of other items. In programming, an array can be seen to similar to the way that variables are stored in memory. In this sense, there is a direct mapping between the variable name or index and the value.
Arrays are incredibly useful ways of storing lots of information in one easy to access place. Not only this, unlike 1-dimension variables (all variables used in this tutorial), array values can be defined at runtime.
As mentioned, arrays can be seen as maps:
An array provides a map from the left (index or key) to something on the right (value)
Arrays in PHP
In PHP, unlike certain other languages, arrays are dynamically sized meaning that the size can change at any point without having to redeclare the array (this may go on in the background in the PHP interpreter but the language itself does not require this).
An array in PHP is declared using the
keyword and displayed using
<?php $array = array(); print_r($array); ?>
This defines an empty array that is, one with no elements within it. The array need not be empty on the instantiation of the array:
<?php $array = array("Firstname", "Surname"); print_r($array); ?>
PHP 5.4 also defines a new syntax that is similar to most other languages (including my own ZPE)
that does not require the use of the
<?php $array = ["Firstname", "Surname"]; print_r($array); ?>
Adding elements to an array
PHP the use of the
array_push function to put an element in the array:
<?php $array = array(); array_push($array, "Value"); print_r($array); ?>
Arrays can also be given a value at a certain index using:
<?php $array = array("Firstname", "Surname"); $array = "Jack"; $array = "Spillburg"; print_r($array); ?>
Arrays can also be given mixed values in PHP, so an array that has a collection of string values can have integers added to it and vice versa.
Retrieving elements from an array
Like many other languages, PHP defines a simple way of getting a value from an array. PHP uses the index (a numeric location in the array) or a key to retrieve a value from the array.
Using the index, the keys start at the number 0 and count up so the first index is 0.
<?php $array = array(); $array = "Jack"; $array = "Spillburg"; echo $array; ?>
This simple PHP script here will print to the screen the value of the array found at index 1.
An associative array is an array in which the key of the map is defined. By default, PHP arrays define a key using an index, but with associative arrays a key is defined, thus creating an association between the key and its value.
The only difference between a standard array and an associative array is that keys are defined by the program.
Declaring associative arrays
Associative arrays can be defined as being empty in the same way as a standard array. In the following case, an array has been defined which has the map of "Jack" to "Adams":
<?php $array = array("Jack" => "Adams"); print_r($array); ?>
Adding elements to associative arrays
Values cannot be added using the
but they can be added using similar syntax as above:
<?php $array["Key"] = "Value"; print_r($array); ?>
Retrieving elements from an associative array
Elements can be retrieved the same way with associative arrays as with standard arrays except that instead of the index, the key is given:
<?php $array = array("Jack" => "Adams"); echo $array["Jack"]; ?>