php – Arrays

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:

$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:

$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:

$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:

$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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: