How to Use One-Dimensional Arrays with PHP for HTML5 and CSS3 Programming

By Andy Harris

The most basic array in PHP is a one-dimensional array, which is basically just one container with slots for HTML5 and CSS3 programming. Each slot has only one variable in it. Here, you find out how to create this type of array and fill it.

How to create an array

Array creation is pretty simple. First, you need to create a variable and then tell PHP that you want that variable to be an array:

$theVar = array();

Now, $theVar is an array. However, it’s an empty array waiting for you to come along and fill it.

Technically, you can skip the variable creation step. It’s still a good idea to explicitly define an array because it helps you remember the element is an array, and there are a few special cases (such as passing an array into a function) where the definition really matters.

How to fill an array

An array is a container, so it’s a lot more fun if you put something in it. You can refer to an array element by adding an index (an integer) representing which element of the array you’re talking about.

Say you have the following array:

$spanish = array();
$spanish[1] = "uno";
$spanish[2] = "dos";

What you do here is to add two elements to the array. Essentially, you say that element 1 is uno, and element 2 is dos.

PHP has another interesting trick available. Take a look at the next line:

$spanish[] = "tres";

This seems a little odd because you didn’t specify an index. PHP is pretty helpful. If you don’t specify an index, it looks at the largest index already used in the array and places the new value at the next spot. So, the value tres will be placed in element 3 of the array.

PHP is somewhat notorious for its array mechanism. Depending on how you look at it, PHP is far more forgiving or far sloppier than most languages when it comes to arrays. For example, you don’t have to specify the length of an array.

PHP just makes the array whatever size seems to work. In fact, you don’t even have to explicitly create the array. When you start using an array, PHP automatically just makes it if it isn’t already there.

How to view the elements of an array

You can access the elements of an array in exactly the same way you created them. Array elements are just variables; the only difference is the numeric index. Here’s one way to print out the elements of the array:

print <<< HERE
One: $spanish[1] <br />
Two: $spanish[2] <br />
Three: $spanish[3] <br />
HERE;

You can simply print out the array elements like any ordinary variable. Just remember to add the index.

Another great way to print out arrays is particularly useful for debugging. Take a look at this variation:

print "<pre> n";
print_r($spanish);
print "</pre> n";

The print_r() function is a special debugging function. It allows you to pass an entire array, and it prints out the array in an easy-to-read format. It’s best to put the output of the print_r() function inside a <pre> element so that the output is preserved.

Of course, the results of the print_r() function mean something to you, but your users don’t care about arrays. This is only a debugging tool. Typically, you’ll use some other techniques for displaying arrays to your users.

To see what all the code in basicArray.php looks like.

image0.jpg

Preloading an array

Sometimes you’ll know the elements that go into an array right away. In those cases, you can use a special version of the array() function to make this work. Take a look at this code.

$english = array("zero", "one", "two", "three");
print "<pre> n";
print_r($english);
print "<pre> n";

This simple program allows you to load up the value of the array in one swoop. Note that this started with zero. Computers tend to start counting at zero, so if you don’t specify indices, the first element will be zero-indexed.

You can use the print_r() function to quickly see the contents of the array.

image1.jpg