Array Variables
PHP Variables
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 start with 0.
In PHP associative arrays use string indices instead of numeric indices. Both types of arrays are created using the array() construct.
Numerically Indexed Arrays
$my_array = array('red', 'green', 'blue');
This code creates a numerically indexed array named $my_array. The array is assigned three string elements – 'red', 'green', and 'blue'. Each element is identified by a numeric index.
$my_array[0] = 'red'; //index 0 corresponds to element 'red'
$my_array[1] = 'green'; // index 1 corresponds to element 'green'
$my_array[2] = 'blue'; // index 2 corresponds to element 'blue'
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>
<html>
<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] . "<br>";
echo "The second value of the array is " . $my_array[1] . "<br>";
echo "The third value of the array is " . $my_array[2] . "<br>";
?>
</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 flexible 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 an index and value is an equal sign immediately followed by a greater than symbol (=>).
$members = array('fname' => 'John', 'lname' => 'Smith', 'age' => 50);
In this example, the array contains three elements and three string indices, 'fname', 'lname', and 'age', are used to indicate the three elements.
$members['fname'] = 'John'; //index 'fname' corresponds to element 'John'
$members['lname'] = 'Smith'; // index 'lname' corresponds to element 'Smith'
$members['age'] = '50'; // index 'age' corresponds to element 50
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>
<html>
<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'] . "<br/>";
echo "The user's last name is " . $members['lname'] . "<br/>";
echo "The user's age is " . $members['age'] . "<br/>";
?>
</p>
</body>
</html>
The user's first name is John
The user's last name is Smith
The user's age is 50
Array Functions
Besides the array() function, PHP includes numerous other functions available for an array. The following are some of the most commonly used array functions. A more extensive list is available at the PHP web site.
- count() : this function is used to count the number of elements in an array.
- sort() this function is used to sort the elements of an existing array.
- shuffle() this function is used to randomize the elements of a given array.
- sizeof() this function is an alias of the count() function.
- array_slice($array_name, offset, length) this function is used to extract a chunk of an existing array. $array_name is the name of the array to slice, offset denotes the position where the slice will begin, length indicates the number of elements that will be sliced from the array.
- array_merge($array_name, $array_name) this function is used to combine or merge two or more existing arrays. The names of the arrays are separated by commas.
The following code shows how each of the array functions are used.
<!DOCTYPE html>
<html>
<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, which is 6
$array_size = sizeof($numbers);
// sorts the elements of the $numbers array
// to array(7, 10, 18, 20, 30, 50)
sort($numbers);
// randomizes the elements of the $numbers array
shuffle($numbers);
// merged two arrays
// and 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
// $slice will include 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. The following are commonly used PHP super global variables:
- - $_GET[]
- - $_POST[]
- - $_REQUEST[]
- - $_COOKIE[]
- - $_FILES[]
- - $_SERVER[]
- - $_ENV[]
- - $_SESSION[]
The super global variables are used for special purposes and will be discussed in later sections. Arrays are frequently used in PHP and other programming in general. This section has introduced the basics of PHP array and described some of the basic functions. This foundational knowledge will be necessary as more advanced array topics are introduced in later sections.