JavaScript Editor Javascript validator     Website design 



Team LiB
Previous Section Next Section

Working with Basic Arrays

Programming is about the combination of control structures (like loops) and data structures (like variables). You've learned the very powerful looping structures. Now it's time to look at a data structure that works naturally with loops. Arrays are special variables made to hold lists of information. PHP makes it quite easy to work with arrays. Look at Figure 4.7. This program, called basicArray.php, demonstrates two arrays.

Click To expand
Figure 4.7: The information displayed on this page is stored in two array variables.

First, I'll let you look over the entire program, then I'll show you how it does its work.

<html>
<head>
<title>
Basic Array
</title>
</head>

<body>

<h1>Basic Array</h1>

<?

//simply assign values to array
$camelPop[1] = "Somalia";
$camelPop[2] = "Sudan";
$camelPop[3] = "Mauritania";
$camelPop[4] = "Pakistan";
$camelPop[5] = "India";

//output array values
print "<h3>Top Camel Populations in the World</h3>\n";
for ($i = 1; $i <= 5; $i++){
  print "$i: $camelPop[$i]<br>\n";
} // end for loop

print "<i>Source: <a href = http://www.fao.org/ag/aga/glipha/index.jsp> Food and Agriculture
Organization of the United Nations</a></i>\n";

//use array function to load up array
$binary = array("000", "001", "010", "011");

print "<h3>Binary numbers</h3>\n";
for ($i = 0; $i < count($binary); $i++){
  print "$i: $binary[$i]<br>\n";
} // end for loop

?>


</body>
</html>

Generating a Basic Array

Look at the lines that describe $camelPop:

//simply assign values to array
$camelPop[1] = "Somalia";
$camelPop[2] = "Sudan";
$camelPop[3] = "Mauritania";
$camelPop[4] = "Pakistan";
$camelPop[5] = "India";

The $camelPop variable is a variable meant to hold the five countries with the largest camel populations in the world. (If this array stuff isn't working for you, at least you've learned something in this chapter!) Since $camelPop is going to hold the names of five different countries, it makes sense that this is an array (computer geek lingo for list) rather than an ordinary variable. The only thing different about $camelPop and all the other variables you've worked with so far is $camelPop can have multiple values. To tell these values apart, use an index in square brackets.

TRICK 

Apparently the boxer George Foreman has several sons also named George. I've often wondered what Mrs. Foreman does when she wants somebody to take out the trash. I suspect she has assigned a number to each George, so there is no ambiguity. This is exactly how arrays work. Each element has the same name, but a different numerical index, so you can tell them apart.

Many languages require you to explicitly create array variables, but PHP is very easygoing in this regard. Simply assign a value to a variable with an index in square braces and you've created an array.

HINT 

Even though PHP is good-natured about letting you create an array variable on the fly, you might get a warning about this behavior on some Web servers (those that have error reporting set to E_ALL). If that's the case, you can create an empty array with the array() function described in the following sections and then add values to it.

Using a Loop to Examine an Array's Contents

Arrays go naturally with for loops. Very often when you have an array variable, you'll want to step through all of its values and do something to each one. In this example, I want to print out the index and the corresponding country's name. Here's the for loop that performs this task:

//output array values
print "<h3>Top Camel Populations in the World</h3>\n";
for ($i = 1; $i <= 5; $i++){
  print "$i: $camelPop[$i]<br>\n";
} // end for loop

Because I know the array indices will vary between 1 and 5 (inclusive), I set up my loop so the value of $i will go from 1 to 5. Inside the loop, I simply print out the index ($i) and the corresponding country ($camelPop[$i]) The first time through the loop, $i will be 1, so $camelPop[$i] is $camelPop[1], which is "Somalia." Each time through the loop, the value of $i will be incremented, so eventually every element of the array will be displayed.

TRICK 

The advantage of combining loops and arrays is convenience. If you want to do something with each element of an array, you only have to write the code one time, then put that code inside a loop. This is especially powerful when you start to design programs that work with large amounts of data. If, for example, I wanted to list the camel population of every country in the UN database rather than simply the top five countries, all I would have to do is make a bigger array and modify the for loop.

Using the array() Function to Pre-Load an Array

Often you'll start out knowing exactly which values you want to place in an array. PHP provides a shortcut for loading up an array with a set of values.

//use array function to load up array
$binary = array("000", "001", "010", "011");

In this example, I created an array of the first four binary digits (starting at zero). You can use the array keyword to assign a list of values to an array. Note that when you use this technique, the indices of the elements are automatically created for you.

TRAP 

Most computer languages automatically begin counting things with zero rather than one (the way humans tend to count). This can cause confusion. When PHP builds an array for you, the first index will automatically be zero, not one.

Detecting the Size of an Array

Arrays are meant to add flexibility to your code. You don't actually need to know how many elements are in an array, because PHP provides a function called count() that can determine how many elements an array has. In the following code, I used the count() function to determine the array size.

print "<h3>Binary numbers</h3>\n";
for ($i = 0; $i < count($binary); $i++){
  print "$i: $binary[$i]<br>\n";
} // end for loop

Note that my loop sentry goes from zero to one less than the number of elements in the array. If you have four elements in an array and the array begins with zero, the largest index will be three. This is a standard way of looping through an array.

TRICK 

Since it is so common to step through arrays, PHP provides another kind of loop that makes this even easier. You'll get a chance to see that looping structure in Chapter 5, "Better Arrays and String Handling." For now, just make sure you understand how an ordinary for loop is used with an array.


Team LiB
Previous Section Next Section


JavaScript Editor Javascript validator     Website design