Page 7
Arrays of Light
One of your best tools now that you've mastered variables you have, haven't
you? are arrays.
Arrays give you the ability to store not just one value inside a variable,
but a whole bunch of values in a single variable. Imagine! Why on earth
would you want to do that? We'll get to that.
If I wanted to catalog all of the animals in my house, I could
set each one as a regular variable. I've got two dogs, Phoebe and Ruby,
and a squirrel that died in the attic last year, whom we'll call Rotty (the
smell was no picnic, let me tell you). Setting each one as a variable looks
like this:
$dog1 = "Phoebe";
$dog2 = "Ruby";
$squirrel1 = "Rotty";
But an array will let us store all these inside one single variable, which
I'll call $critters. Each element of the variable has its own "key"
that is used to access that part of the array, which can either be a string
of letters or numbers.
Let me explain the "key" concept another way: If we're storing
three different values inside one variable (like storing Phoebe, Ruby, and
Rotty inside $critters), we need some way to be able to suck out any individual
part of the array to use it. An array will automatically number each element
that comprises it, so the key can be element 1, element 2, and element 3.
Or, as we'll see later on, we can name each part of the array with text.
In this case I could make the keys "fat dog," "skinny dog,"
and "squirrel" and use those to identify each array member.
Let's make a simple array and then use it. The easiest way to create an
array is to use the array() function, which assigns a bunch of values to
your array at once and looks like this:
$critters = array ( "Phoebe", "Ruby", "Rotty"
);
This stores all my animal names into one variable ($critters)
in an array, and automatically assigns a numbered "key" to each
element starting in order and giving the first element the number 0. So
Phoebe is element [0], Ruby is [1], Rotty is [2], etc. I make up the name
of the array myself (here it's $critters).
You can now get at any of the array elements by referring to the variable
followed by the element number in square brackets: $critters[0],
for example. Here it is in action:
<?php
print "$critters[2]";
?>
This will simply print the third element in the array, which is Rotty
(don't forget that array numbers start at 0, so $critters[2]
is third after $critters[0] and $critters[1]).
There's another way to set an array, or even to add to an existing array,
by setting each element individually:
$critters[] = "Phoebe";
$critters[] = "Ruby";
$critters[] = "Rotty";
This'll have the same effect as using the array() function, giving the
first element the key [0] and so on. But wait! I forgot about Opie the cat.
Hmmm. Regardless of how we made the array in the first place, I can easily add Opie
like this:
$critters[] = "Opie";
PHP is smart enough to count the number of elements and give Opie the next
available one, which in this case (after Phoebe, Ruby, and Rotty) is [3].
To recap this concept, I can set an array to include the animals in my
house either this way:
$critters[] = "Phoebe";
$critters[] = "Ruby";
$critters[] = "Rotty";
$critters[] = "Opie";
Or this way:
$critters = array ( "Phoebe", "Ruby", "Rotty",
"Opie" );
Both will be indexed in the computer brain with the values:
$critters[0] = "Phoebe";
$critters[1] = "Ruby";
$critters[2] = "Rotty";
$critters[3] = "Opie";
And in both cases, you could get at any element in the array by describing
its number ...
<?php
print "$critters[3]";
?>
... which would print the string Opie to the window of your browser.
Arrays can be made to do all kinds of things, like being incremented by
number, sorted in alphabetical order, printed by different types of categorization,
and many more.
Associative Arrays
Ready to get more complicated? The associative array indexes the contained
elements not by numbers, but by names that you determine. Inside the array()
function, you set up pairs where you name the key and its value using the
combo of the "=" and the ">", like: key=>"value".
Here's what it looks like in action:
$PhoebeDog = array (
name=>"Phoebe",
description=>"fat dog",
color=>"grey and white",
age=>7
);
Here we're telling the array to create the keys "name," "description,"
"color," and "age"; and we give each of those keys a
value (name is "Phoebe", description is "fat dog," and
so on).
We can get at any part of the array through the "key" names that
we set, for example:
print $PhoebeDog[color];
will give us grey and white. We can also set each key individually,
like so:
$animals[name] = "Phoebe";
$animals[description] = "fat dog";
$animals[color] = "grey and white";
$animals[age] = 7;
Finally, let's make it hurt. We're going to get some serious power out
of this arrays business by creating a "multi-dimensional" array.
A multi-dimensional array is an array (say the animals in my house) that
is made up of other arrays (for each animal, an array that contains the
critter's name, color, description, and age).
We make multi-dimensional arrays by creating one array:
$animals = array
(
);
...and then we fill that array with an array of animals in which we've
defined the keys, like this:
$animals = array (
array ( name=>"Phoebe",
type=>"dog",
color=>"grey and white",
age=>7 ),
array ( name=>"Ruby",
type=>"dog",
color=>"brown and white",
age=>7 ),
array ( name=>"Rotty",
type=>"squirrel",
color=>"grey",
age=>2 ),
array ( name=>"Opie",
type=>"cat",
color=>"grey tabby",
age=>5 )
);
To use this now, we can get any part of the information contained in there
by naming the overall array ($animals), naming the number of the sub-array
that we want to find out about (Phoebe is [0], Ruby is [1], etc.) and then
naming the key for the attribute we want to get at (name, type, color, or
age).
To find out the age of the cat, we'd write:
print $animals[3][age];
Here's what it all looks like together. This is all in one page, but remember
that you can set arrays in one place (in code or in form fields from another
page or in a database, say) and get at the info contained within from somewhere
else. Here I'm putting it all together on one page so you can see it all
at once.
<html>
<head>
<title>Pet Arrays</title>
</head>
<body>
<?php
$animals = array (
array ( "name" => "Phoebe",
"type" => "dog",
"color" => "grey and white",
"age" => 7 ),
array ( "name" => "Ruby",
"type" => "dog",
"color" => "brown and white",
"age" =>7 ),
array ( "name" => "Rotty",
"type" => "squirrel",
"color" => "grey",
"age" =>2 ),
array ( "name" => "Opie",
"type" => "cat",
"color" => "grey tabby",
"age" => 5 )
);
print $animals[2]["type"]; print ("<br>"); print $animals[3]["color"];
?>
</body></html>
Now see what it does.
What we've just done is create an array that includes a sub-array for each
animal which contains detailed info about that critter; then we print a
sentence that uses the type and ages of two of the animals.
next page»
|