PHP Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 3.00

What Is an Array?

This section describes what is an array - an ordered pairs of keys and values. If sequential integer keys are used, an array is a simple indexed list. If string keys are used, an array is a map.

What Is an Array? An array is a data type that represents an ordered pairs of keys and values. Arrays in PHP are different than arrays in most of other languages. Basic rules for PHP's arrays are:

1. An array can be constructed by the array constructor, like:

   $myArray = array(k1=>v1, k2=>v2, ..., kn=>vn);

where k1, k2, ..., and kn are keys of integer type or string type. v1, v2, ..., and vn are values of any data types.

2. The value of a given key in an array can be expressed by the array variable followed by the key in square brackets, like:

   print $myArray[kn];

3. The value of a given key in an array can be modified by an assignment statement, like:

   $myArray[kn] = new_value.

4. A new pair of key and value can be added to an array by an assignment statement, like:

   $myArray[kx] = vx

5. If a string key represents an integer, it will be used as an integer key. So $myArray['7'] is the same as $myArray[7].

6. If the key is missing in an array assignment or an array constructor, an integer key of 0, or the highest integer key plus 1, will be provided. So $myArray[] = "Last" assigns "Last" to a new integer key.

7. An empty string is also a valid string key, like:

   $myArray[''] = vy; 

8. A pair of key and value can be removed by using the unset() function, like:

   unset($myArray[kn]); 

9. Pairs of keys and values are ordered in an array like a queue. New pairs are always added at the end of the array.

Last update: 2005.

Sections in This Chapter

What Is an Array?

Creating Arrays - Examples

Array Related Built-in Functions

Dr. Herong Yang, updated in 2009
What Is an Array?