While a PHP scalar variable stores a single value, an array variable can be used to store a set or sequence of values. PHP supports numerically indexed arrays and associative arrays. An array in PHP is actually an ordered map. A map is a type that maps values to keys. Array variables consist of two parts - an index and an element. The array index, sometimes referred to as the array key, is a value used to identify or access array elements. The array index is placed in square brackets. Most arrays use numeric indices that typically start with 0 or 1. In PHP associative arrays can use string indices. Both types of arrays are created using the array() construct.
Numerically Indexed Arrays
This code creates a numerically indexed array called $my_array. The array is assigned three elements - red, green, and blue. Each element is identified by a numeric index.
To access the contents of an array, use the array name and index. The following code is used to display the values of the $my_array variable.
<!DOCTYPE html PUBLIC "-//W3C//DTD/XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml11-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>A Web Page</title>
</head>
<body>
<p>
<?php
$my_array = array('red', 'green', 'blue');
echo "The first value of the array is " . $my_array[0];
echo "The second value of the array is " . $my_array[1];
echo "The third value of the array is " . $my_array[2];
?>
</p>
</body>
</html>
The first value of the array is red The first value of the array is green The first value of the array is blue
Associative Arrays
Associative arrays allow you to use more useful index values. With numerically indexed arrays, index values are created automatically beginning with 0. Associative arrays permit numeric and string index values. The symbol between the index and values (=>) is an equal sign immediately followed by a greater than symbol.
In this example, the array members contain three elements, however the string indices - FName, LName, and Age are used.
To access the contents of an array, use the array name and index. The following code is used to display the values of the $members variable.
<!DOCTYPE html PUBLIC "-//W3C//DTD/XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml11-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>A Web Page</title>
</head>
<body>
<p>
<?php
$members = array('FName' => John, 'LName' => Smith, 'Age' => 50);
echo "The user's first name is: " . $members['FName'];
echo "The user's last name is " . $members['LName'];
echo "The user's age is " . $members['Age'];
?>
</p>
</body>
</html>
Besides the array() function, PHP includes numerous other functions for use with arrays. The following section describes some of the most common functions. A more extensive list is available at the PHP web site.
The following code shows how each of the array functions are used.
<!DOCTYPE html PUBLIC "-//W3C//DTD/XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml11-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>A Web Page</title>
</head>
<body>
<p>
<?php
//Two arrays are created
$numbers = array(50,20,18,30,10,7);
$colors = array('red', 'blue', 'green')
// determines the size of the array $numbers - 6
$array_size = sizeof($numbers);
// sorts the elements of the $numbers array - returns array(7,10,18,20,30,50)
sort($numbers);
// randomizes the elements of the $numbers array
shuffle($numbers);
// $merged_array returns array(7,10,18,20,30,50,'red','blue','green')
$merged_array = array_merge($numbers,$colors);
// slice the numbers 18 and 20 from the sorted $numbers array
// array $slice returns array(18,20)
$slice = array_slice($numbers, 2, 2);
?>
</p>
</body>
</html>
PHP also includes a number of pre-defined or global arrays. These are also known as super-global variables because they are always present and available to all PHP script blocks. Following are commonly used PHP superglobal variables:
The PHP superglobal variables will be described in later tutorials. Arrays have many uses in PHP and programming in general. This section has introduced some of the basic PHP array topics and described some basic functions. This foundation will be necessary as more advanced array topics are introduced in later sections.