1

PHP Basics – The Array

Posted April 20th, 2009 in PHP, Programming and tagged , , , , by Aaron

One of the fundamental variable types, yet one of the more complex when you are beginning, is the Array. The array just takes a little bit to wrap your mind around, or maybe a good analogy as to exactly what it is doing.

You can pretty much think of an array as a mini-storage unit for variables, someplace to order them, keep them in one space, and reference them by name dynamically. There are more advanced ways to do some of this as well but arrays are a good starting point.


Just as a starter, here is a basic array, the first code snippet is the PHP to write the array, and the second is a visual representation of the variable.

 $variable = array("red","green","blue");
$variable = Array
(
    [0] => red
    [1] => green
    [2] => blue
)

Now, a brief overview at exactly what you are looking at here, in case you have never worked in an array before. The variable named $variable is an array, and it contains 3 values, “Red”, “Green” and “Blue”. Each of these values now has dynamically been assigned a number on the left hand column. These are the keys to access the data in the array.

If you were to make an analogy to understand it better, we could say an array is a book, and each variable in the array is a page. Each page has a page number, and those page numbers would be the keys in the array. Let’s run with that example, and create a bit more complex of an array, so and pretend our array is indeed a book.

 $book = array(
     "The quick brown dog...",
     "Jumped over the big red fence...",
     "To get to his food..."
  );

Don’t let the change of how I typed the code throw you off, this is the exact same sample as above, instead we have a “page” of the “book” stored within each “page number”. Notice how the variable looks now:

Array
(
    [0] => The quick brown dog...
    [1] => Jumped over the big red fence...
    [2] => To get to his food...
)

The variable is still pretty much the same, just with different values stored within its keys. So now, if we were going to print out the first value of the array to the screen, we would echo it out, with slightly different syntax than just a standard echo.

 echo $book[0];

You would echo it just like a standard variable, except directly after, you would use brackets to specify which key you want to retrieve from the array. The above code would output “The quick brown dog…” onto the screen. You can simply change the number at the end of the variable to specify which portion of the array you are looking for.

The above syntax you can also set values in an array with as well!

 $book[4] = "This is another page!";
 $book[1000] = "This is WAY out there...";

These two lines of code will write their values into the key of 4 and key of 1000. There’s nothing telling you that you have to use numbers in order, or even numbers for that example!

 $book['index'] = "This is the index of the book";

You can store keys with words as well, if that helps you organize how your data is stored. PHP uses a lot of arrays for sessions, server variables and many other things. Here’s an example of pulling the IP address of your client, from a $_SERVER variable that is automagically set whenever a script on your server is called.

 echo $_SERVER['REMOTE_ADDR']; // Will print the IP address accessing the script

Arrays can be an amazing storage tool to keep your variables in check, instead of assigning every single variable its own name. It also is nice if you want to pass multiple variables into a function or an object’s method at once. That’s a little bit more advanced, and a subject for another time, but just understanding the basics of how an array looks, visually and in concept, will take you a long ways.

One Response so far.

  1. [...] For more information on arrays, please see: PHP Basics – The Array. [...]

Leave a Reply