Assigning a collection of values to an array variable is simple when using the array() construct:
$co lors = array("blue","indigo","yellow");
Or, if you know that you want to create an array $
colors but don”t yet know what values to fill it with, create an empty array:
$colors = array();
Adding new v
alues to the array is a breeze:
$colors[] = "hunter green";
Now the array $colors contains four v
alues. Oftentimes, you need to access a single item in an array, such as for output or a calculation. To do this, you need to refer
to a key, which leads to the value you want. We haven”t created any keys ourselves in this example, and so PHP has created n
umeric keys: The key for the first item (“blue”) is zero, the key for the second item is 1, and so on, with the key for th
e last item in a list being the number of items minus 1, since keys begin at zero. So, we can output the second color in the array v
ia the key 1:
print $colors[1];
… will output “indigo”. This type of indexed key system is great when
you want to keep items in a specific order, but it is also limiting because the keys do not really mean anything. How do we k
now we want the second key? In some applications we do know … in others, this thinking just doesn”t work. The alternative is to c
reate keys that are meaningful labels. For example, suppose our collection of colors was really a list of colors for our car. A car
may have several colors, depending on the part of the car — exterior, trim, fabric, dashboard. Here it makes sense to use keys whic
h are labels more meaningful than a mere index:
$colors = array("exterior"=>"blue,"
"trim&q uot;=>"indigo,"
"fabric"=>"yellow,"
"dashboard"=>"hunter green");
Admittedly, this is one ugly car. Our list items have gained meaning but lost order — which is fine, since this list is n
ot about order. It”s now easy to output the fabric color of this car, because “fabric” is a key in the list:
pri nt $colors[fabric];
… will output “yellow.”
Once an array has been built, typically it must somehow be
manipulated, so as to sort it or simply output each of its values. PHP contains a variety of functions for working with an array. Mo
st of these will not be covered here. Let”s say, though, that we wanted to output each of the items in $colors, along with t
he key associated with that item. The general procedure for “stepping through” an array and processing each of its items w
ill apply to many arrays you use in the future — let”s use this procedure on $colors.
while (list($key,$value) = e ach($colors)) {
print "$key: $value
";
}
Although we have not yet discussed the while loop
yet, bear with us! The above code sets up a loop, or a series of repeating steps. The each() function returns a list (array)
of values corresponding to the key and value for a single item in the array. These returned values are assigned on-the-fly to $k
ey and $value, courtesy of the list() function. The print statement simply outputs the key and value for th
e current item in the list — of course, you could do much more complex things with these values at this point in the code. Each tim
e the loop returns to the while statement, it gets the next item in the array. When the array is completed the while l
oop will end.
In the browser, the above code would output:
exterior: blue trim: indigo fabric: yellow dashboard: hun ter green
Notice the order of our output, though — same order that these items were defined earlier in our original arr
ay() statement. Suppose instead that we”d like to sort this array alphabetically by key, so that “dashboard” appears
first and “trim” last. Simply, use PHP”s ksort() function to sort $colors by key, and then step through the
array as before:
ksort ($colors); while (list($key,$value) = each($colors)) {
print "$key: $value
" ;
}
The PHP reference manual details a variety of ad
ditional functions for managing your data arrays and performing nifty acrobatics.