If you've ever created large scripts that require many variables (sometimes nearly 100), you know
what it's like trying to keep track of what each variable contains and what it's used for. Trust me,
I've been there. If we can store those variables inside other variables then the list of hundereds
shrinks to less than 10. That's where arrays come in.
An array, broken down to its simplest form, is simply a variable that holds
variables. It's kind of like a bunch of houses in a city. The city contains many houses and each house
has an address. In the same manner, each variable (house) within the array (city) has its own address
called an index.
Let's say you have the names of three persons (John, Susie, and Dave)
stored in the variables named $sPerson1, $sPerson2,
and $sPerson3 respectively. Now you can use
those three variables within your program, but it's easy to lose track of which one is which...
especially if you have other variables. To compact the three variables into an array, you'd do something like this:
<?php
$arrayPeople = array("John", "Susie", "Dave");
?>
Now instead of using $sPerson1, $sPerson2,
and $sPerson3 I can use $arrayPeople. Note
how I created the array using the array() function included with PHP.
If those three names were numbers instead, I wouldn't surround the number with quotes. So now to print
those three names I'd do this:
<?php
$arrayPeople = array("John", "Susie", "Dave");
print $arrayPeople[0];
print $arrayPeople[1];
print $arrayPeople[2];
?>
Why did he start with zero? Because that's where the index starts. Whatever
gets put into the array is assigned an index of zero(0) and that number automatically increments from there.
You can manually assign an index for a particular entry, but I'll cover that later. For now, I'll show you
how to use a loop to automatically print the contents of an entire array:
<?php
$arrayPeople = array("John", "Susie", "Dave");
$nArraySize = count($arrayPeople);
for($index=0; $index < $nArraySize; $index++) // max. index is always number of entries - 1
// because index starts at zero
{
print $arrayPeople[$index];
}
?>
In this case, $index is the index (address) for the entry and
$nArraySize is the number of entries in the array. The count()
function returns the number of entries in the array. Now for very small array like the one I just
used, using a loop actually uses more code, but when you start dealing with arrays that have hundereds
or even thousands of entries (they do exist), you'll be glad that you're using a loop.
This is where I talk about creating your own indexes for array entries.
Whenever I use SESSIONS for administrator access areas of my website, I use an array to store
session information. Here's what the code looks like:
<?php
$SESSION= array(); // that creates a blank array
$SESSION["username"] = $sUserName;
$SESSION["password"] = $sPassword;
$SESSION["accesslevel"] = $nLevel;
// etc,etc,etc.
?>
You see how I used words to identify the index? This way I know that
$SESSION["username"] contains the username of the person.
It's a lot easier than trying to remember that $SESSION[0] contains
the username. My way of coding arrays like this is to use the name of the variable as the index
for that entry. So to store $nDaysinMay in array
$arrayDays I'd put it in $arrayDays["nDaysinMay"].
This way I can even keep track of which variable that entry contains. Now this is where I can dive into multi-dimensional arrays.
Multi-dimensional arrays. Sounds like it came from some 1950s Sci-Fi novel. Anywho, multi-dimensional arrays
are simply arrays within arrays. You remember eariler how I was putting variables into arrays? Well now I'm
stuffing arrays into arrays. For example, let's say I run an on-line bookstore and I sell novels, kid's books,
and magazines, and I put the names of those into arrays. In code, it'd look like:
<?php
$arrayNovels = array("Andromeda Strain", "Rainbow Six", "Lord of the Rings");
$arrayKidBooks = array("Jack and Jill", "Clifford the Big Red Dog", "How Stuff Works");
$arrayMagazines = array("Motorcycle World", "Guitar One", "Car and Driver");
?>
Now this is fairly condensed, but I can stuff all three arrays into an array called
$arrayInventory. Using what I've already talked about, this is how it's done:
<?php
$arrayNovels = array("Andromeda Strain", "Rainbow Six", "Lord of the Rings");
$arrayKidBooks = array("Jack and Jill", "Clifford the Big Red Dog", "How Stuff Works");
$arrayMagazines = array("Motorcycle World", "Guitar One", "Car and Driver");
$arrayInventory = array();
$arrayInventory["arrayNovels"] = $arrayNovels; // this is the same as index 0
$arrayInventory["arrayKidBooks"] = $arrayKidBooks; // this is the same as index 1
$arrayInventory["arrayMagazines"] = $arrayMagazines; // this is the same as index 2
?>
So now I can print them out using for-loops (assume that the above code is included):
So there you have it. You can now create, fill, and use arrays for all your scripting needs. Drop me a line at
the address above if you have a specific question about arrays or PHP in general. Thanks, and may the
code be with you.