Page 135 - Open Soource Technologies 304.indd
P. 135
Unit 6: Arrays
Arrays can be used in many ways to store and organize data quickly and efficiently. It is one Notes
of the more useful data types available to any programming language.
Arrays can most easily be described as an ordered list of elements. You can access the individual
elements by referring to their index position within the array. The position is either specified
numerically or by name. An array with a numeric index is commonly called an indexed array
while one that has named positions is called an associative array. In PHP, all arrays are associative,
but you can still use a numeric index to access them.
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This
type is optimized for several different uses; it can be treated as an array, list (vector), hash table
(an implementation of a map), dictionary, collection, stack, queue, and probably more. As array
values can be other arrays, trees and multidimensional arrays are also possible.
6.1 Indexed versus Associative Arrays
There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are
integers, beginning at 0. Indexed arrays are used when you identify things by their position.
Associative arrays have strings as keys and behave more like two-column tables. The first column
is the key, which is used to access the value.
PHP internally stores all arrays as associative arrays, so the only difference between associative
and indexed arrays is what the keys happen to be. Some array features are provided mainly for
use with indexed arrays; because they assume that you have or want keys that are consecutive
integers beginning at 0. In both cases, the keys are unique, that is you cannot have two elements
with the same key, regardless of whether the key is a string or an integer.
PHP arrays have an internal order to their elements that is independent of
the keys and values, and there are functions that you can use to traverse
the arrays based on this internal order. The order is normally that in which
values were inserted into the array.
6.1.1 Associative Arrays
Associative arrays are the arrays that use named keys that you assign to them. In the products
array, we allowed PHP to give each item the default index. This meant that the first item we
added became item 0, the second item 1, and so on. PHP also supports associative arrays. In an
associative array, we can associate any key or index we want with each value.
Initializing an Associative Array
The following code creates an associative array with product names as keys and prices as values.
$prices = array( ‘Tires’=>100,
‘Oil’=>10, ‘Spark Plugs’=>4 );
Accessing the Array Elements
Again, we access the contents using the variable name and a key, so we can access the information
we have stored in the prices array as $prices[ ‘Tires’ ], $prices[ ‘Oil’ ], and$prices[ ‘Spark Plugs’ ].
Like numerically indexed arrays, associative arrays can be created and initialized one element
at a time.
The following code will create the same $prices array. Rather than creating an array with three
elements, this version creates an array with only one element, and then adds two more.
LOVELY PROFESSIONAL UNIVERSITY 129