PHP arrays are associative arrays with a little extra machinery thrown in.
The associative part means that arrays store element
values in association with key values rather than in a
strict linear index order. (If you have seen arrays in other programming languages, they are likely to have been vector arrays rather
than associative arrays.) If you store an element in an array,
in association with a key,all you need to retrieve it later from
that array is the key value. For example, storage is as
simple as this:
$state_location[‘San Mateo’] = ‘California’;
which stores the element ‘California’ in the array variable $state_location,
in association with the lookup key ‘San Mateo’. After this has been stored,
you can look up the stored value by using the key, like so:
$state = $state_location[‘San Mateo’]; // equals ‘California’
If all you want arrays for is to store key/value pairs, the preceding
information is all you need to know. Similarly,
if you want to associate a numerical ordering with a bunch of values,
all you have to do is use integers as your key values, as in:
$my_array[1] = “The first thing”;
$my_array[2] = “The second thing”;
Post new comment