Lesson Weekend

Often, we'll want to use collections of numbers and words, called arrays. To explore arrays, let's create a program that lets us write a grocery shopping list. First, how do we store a bunch of foods? Try this in the PHP shell:

> $groceries = array("eggs", "milk", "bread");
> var_dump($groceries);
array(3) {
  [0]=>
  string(4) "eggs"
  [1]=>
  string(4) "milk"
  [2]=>
  string(5) "bread"
}

First, we create a variable called $groceries to hold our list. Then we use a built-in function called array to actually create the list. array takes any number of arguments - they will be the elements in your list. They can be words, numbers, or anything. Our array currently holds three strings: "eggs", "milk" and "bread".

We can see the contents of the array by using a new function called var_dump. var_dump is just like echo except that you have to put whatever you want to print into parentheses next to it. echo does not use parentheses, and it does not work with arrays. var_dump will print anything, but because it is very verbose it is usually used for debugging, while echo is generally used for printing to the browser. If we try to echo $groceries;, nothing will show up; if we try to var_dump("eggs");, we'll get string(4) "eggs", which we wouldn't want showing up on our webpages.

When we look at our grocery list, we can see that each item has a number associated with it. This number is called the item's index. The index keeps the list in a particular order. The array function reads its arguments from left to right and counts from 0. So "eggs" are assigned to index 0 - they are the first argument to the array function so they are first in line. Next "milk" is assigned to index 1, and "bread" is assigned to index 2.

Now, suppose I just remembered that we also need to buy apples and tomatoes. Let's add them to the array - type this into the PHP shell next.

> array_push($groceries, "apples", "tomatoes");
> var_dump($groceries);
array(5) {
  [0]=>
  string(4) "eggs"
  [1]=>
  string(4) "milk"
  [2]=>
  string(5) "bread"
  [3]=>
  string(6) "apples"
  [4]=>
  string(8) "tomatoes"
}

We have used the array_push function to add 2 more items to the end of our array, which means they are assigned to indexes 3 and 4. As you can see, array_push takes at least two arguments. The first one tells it which array needs to be modified, and any other arguments are added to the end of the array.

What if I have purchased everything on the grocery list except for the third item? I will need to ask our array what it is.

> $third_item = $groceries[2];
> echo $third_item;
bread

To get a particular item out of an array, you put its index into square brackets [ ] next to the variable that is holding the array. We can use echo here because we are only printing out one item, not the entire array.

Remember that array indices count from 0. So the third item actually has index 2.

Let's go into the PHP shell and make a list of the Lord of the Rings books by J.R.R. Tolkien.

> $tolkien_books = array("The Hobbit", "Fellowship of the Ring", "The Two Towers", "Return of the King", "The Silmarillion");
> var_dump($tolkien_books);
array(5) {
  [0]=>
  string(10) "The Hobbit"
  [1]=>
  string(22) "Fellowship of the Ring"
  [2]=>
  string(14) "The Two Towers"
  [3]=>
  string(18) "Return of the King"
  [4]=>
  string(16) "The Silmarillion"
}

An array is handy for this because these books take place in a particular chronological order. Since an array keeps its items in order counting from 0, we can ask it what the third book in the series is like this:

> echo $tolkien_books[2];
The Two Towers

Or, when we decide that "Silmarillion" is too long and dull, we can remove this item from the array by using a function called array_splice.

> array_splice($tolkien_books, 4, 1);  // remove 1 element after index 4 of $tolkien_books.
> var_dump($tolkien_books);
array(4) {
  [0]=>
  string(10) "The Hobbit"
  [1]=>
  string(22) "Fellowship of the Ring"
  [2]=>
  string(14) "The Two Towers"
  [3]=>
  string(18) "Return of the King"
}

array_splice requires three arguments - first the array that we are modifying, then the index where we want to start removing items, and the number of items we want removed. "Silmarillion" is at index 4 and it is the only book we want to remove, so the third argument will be 1.