Arrays in PHP: Part 1

06/07/2001

This article will introduce the PHP array data type and discuss the fundamentals of working with arrays.

What exactly is an array?

An array is a variable type in PHP that allows the programmer to associate one value directly to another value or simply take a set of data and organize it into a list format (much like a table). It can be used in many ways to store and organize data quickly and efficiently. It is one of the more useful data types available to any programming language.

Let's say you are a developer who would like to find a way to store your favorite web site addresses using PHP. One highly discouraged method of doing so would be something like the following:

Effective, but such a method is hardly efficient. In situations like this, an array proves useful. The syntax of an array variable is almost identical to the syntax of any other variable in PHP. It follows the same rules regarding variable names, and in many circumstances can be referenced in the same way as any other variable. There are, however, distinct differences in how the data within an array is accessed and how an array variable is created. We'll discuss how to create an array variable first.

When creating an array variable, there are two separate ways to declare it. The first should be used when the data the array will be storing can be hard-coded into the script (static data) and the second is used in circumstances when the data is read or generated during the execution of the script. It's up to you, the developer, to determine what format is right for you. With that in mind, let's take our above example and convert the data into an array format.

Using array() to construct a static array

Array() is one of the methods PHP provides to create and populate an array with data. It is a PHP statement that takes your input and returns an array variable containing that input. Although the general format never changes, there is much more to this statement that we'll be covering in our next issue. For now, let's take a look at the general syntax of the array() statement:

Array array([index]=>[value], [index2]=>[value], ...);

where for the purpose of this article index represents any integer and value represents any data type, variable or otherwise, in PHP. Remember this is a very limited look at the array() statement. In the next article, we'll return to this declaration to show you new and more useful ways to use this statement for complex data storage. It's also important to note that arrays in theory have no size limits imposed on them. Rather, they are limited by the storage capacity of the server running the script.

Now that we have an idea of what an array() statement looks like, let's look at how our earlier example can be stored as an array:

The result is a single variable, $mysites, that contains an indexed list of all three web sites from our earlier example. Notice that the third entry in our array (index #2) was assigned its value from the $site3 value. Therefore, the actual value stored at index #2 will be http://www.coggeshall.org. Now that we know how to construct a basic array, how can we access its values?

Accessing array data

Now that we've constructed our array, we'll discuss how we can retrieve our data from the array. To retrieve a specific value from our array, we need to reference the array variable and provide the specific index of the value we would like to retrieve. Let's say we wanted to access the third value (index #2) in our array, and echo it to the screen -- here's how we would do it:

<?php echo $mysites[2]; ?>

Notice that this looks very similar to the echoing of any variable to the browser except when dealing with an array, we append brackets ( '[' and ']' ) to the end of the variable and reference the index of the value we would like to retrieve between them. We could access the first two values in our array just as easily by replacing the value "2" with 0 or 1.

Constructing an array dynamically

When the data to be stored in an array is not known at the time the script executes, we'll need to construct the array dynamically. This would be rather inefficient and difficult to do using the array() statement. Instead, PHP has provided us a number of what I consider "informal" means of constructing arrays and populating the data. The first method is very similar to assigning any other variable in PHP and incorporates the array-reference syntax we outlined above. Say you would like to create a new entry in our previously created array of web sites. This entry you would like to have an index of "3" and a value of "http://www.php.net". You could append the existing array in the following fashion:

<?php
$mysites[4] = "http://www.php.net";
?>

Now $mysites contains all three of the previously stored values plus the new fourth value you just added. Using this method of assignment, you can create new records or modify existing records with ease. Further, we can omit the actual index value and PHP will automatically assume the next sequential index value as illustrated in the next example:

<?php
$mysites[] = "http://www.zend.com";
?>

The above will automatically store the web site at index #5.

Final notes

Although we have only touched upon the power of arrays in PHP, already you have learned a very useful and powerful tool to assist you when developing PHP scripts. In the next column in this series, I'll discuss more advanced techniques such as associative (or hash) arrays as well as a new control structure that will help you traverse arrays quickly and painlessly. See you then!

John Coggeshall
is a a PHP consultant and author who started losing sleep over PHP around five years ago.