To use this website fully, you first need to accept the use of cookies. By agreeing to the use of cookies you consent to the user of functional cookies. For more information read this page.

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 system

An array provides a map from the left (index or key) to something on the right (value)

Arrays in PHP

Declaring arrays

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 array keyword:

PHP
<?php
	$array = 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
<?php
	$array = array("Firstname", "Surname");
?>
		

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 array function:

PHP
<?php
	$array = ["Firstname", "Surname"];
?>
		

Adding elements to an array

PHP the use of the array_push function to put an element in the array:

PHP
<?php
	$array = array();
	array_push($array, "Value");
?>
		

Arrays can also be given a value at a certain index using:

PHP
<?php
	$array("Firstname", "Surname");
?>
		

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
<?php
	$array = array();
	...
	echo $array[1];
?>
		

This simple PHP script here will print to the screen the value of the array found at index 1.

Associative arrays

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 it's value.

The only difference between a standard array and an associative array is that keys are defined by the program.

Declaring associative arrays

Associatve 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
<?php
	$array = array("Jack" => "Adams");
?>
		

Adding elements to associative arrays

Values can be added using the array_push function but they must follow the same syntax as above:

PHP
<?php
	array_push($array, "Key" => "Value");
?>
		

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
<?php
	$array = array();
	echo $array["Key"];
?>