An Overview of Arrays in PHP

by Matt Wade

In this article I will explain arrays and how they are used in PHP.

Page 1

An Overview of Arrays in PHP by Matt Wade

Arrays in PHP
The PHP Manual defines an array in PHP as an ordered map. Personally, I don't like this definition. I will define an array in PHP as a collection of variables, which I think is a little more descriptive. In most languages, atleast the ones I am familar with, the variables in this collection must all be of the same type. Not so in PHP! This is because of PHP's ( Type Juggling. I'm not going to go into great detail about Type Juggling, but basically it means that you don't declare a variable as a certain type. The context in which the variable is used determines its type. So, how do you assign values to arrays? The exact same way as you would any other variable, except that you specify which element of the array you wish to work with. One important thing to note is that, by default, arrays start numbering their elements at zero. Check this out:
<?php $array[0] = 3; $array[1] = 6; $array[2] = 2; ?>

A cool feature of PHP is the ability to use associative arrays. Associative arrays give you the ability to name your array's keys. So, instead of having $array[0] and $array[1] you can have $array['something'] and $array['anotherthing']. So what's the big deal? Why would I want to use arrays rather than just regular old variables? Imagine this: You are working with customer's names. You have 5 of them that you continually need to echo out. With regular variables, you would do it like this:
<?php echo echo echo echo echo ?> "$customer1<BR>\n"; "$customer2<BR>\n"; "$customer3<BR>\n"; "$customer4<BR>\n"; "$customer5<BR>\n";

Pretty long winded. Imagine doing that with 100 customers. Or 1000. With arrays, you can use a foreach loop. What a foreach loop does is loop as many times as you have elements in an array. Suppose you had an array called $customer that contained your customer names. Check this out:

Page 2

An Overview of Arrays in PHP by Matt Wade

foreach($customer as $value) { echo "$value<BR>\n"; } ?>

Now how easy is that? That code will not change if you have 100 or 1000 customers. That's the beauty of arrays. Ok, that clues you in on some simple arrays. What about multi-dimensional arrays? What are they? Let's take a look at a two-dimensional array as an example. Say you are dealing with selling books. You want to place the names and prices of these books into an array. What you would want to do is create an array where each element is an array. Sound confusing? It really isn't. Take a look at this example:
<?php $books = array(0=>array('name'='A Book','price'=>9.99),1 =>array('name'='Another Book','price'=>17.99)); ?>

Now, here are a couple ways to access that data in our two-dimensional array:
<?php echo $books[0]['name']; echo $books[1]['price']; $books[0]['price'] = 12.99; foreach($books as $onebook) { echo $onebook['name'] . " sells for $". $onebook['price' ] . "<BR>\n"; } ?>

There you have it. A simple introduction to arrays in PHP. I hope it was helpful to you. For more info, check out the PHP Manual. There are a whole bunch of functions in there that can be used with arrays.

Page 3

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.