PHP Arrays

An array is a data structure for grouping values. Each value is associated with a key that can be used to reference it. Arrays are pretty common across programming languages, but PHP arrays have some quirks that can sometimes throw a new programmer off.

For one thing, since PHP is so loosely typed, you can define a variable as an array just by starting to use it as one. For another, PHP arrays can hold mixed types. That means an array can hold objects, integers, strings, booleans, even other arrays. And PHP arrays can expand as needed. There’s no need to declare in advance how big an array is or take any special action to increase its size once you start using it. And PHP supports both numerically-indexed arrays and associative arrays – that is, arrays that have strings for keys.

Let’s start by looking at the basic array creation syntax:

$a1 = array();

The function ‘array’ is actually a language construct. When it’s called this way, it generates a new, empty array, and assigns it to the variable $a1. In order to add values to it, you use square brackets. Here’s one way to do that:

$a1[] = 5;

In this case, the array gets the value 5. And since no key was specified, it automatically just gets the next number based on a 0-indexed sequence. Since in this case the 5 is the first value in the array, it gets assigned to the first index 0. If I want, I can specify the key for an element I add:

$a1[3] = 10;

And now the array has 2 elements. The key 0 refers to the value 5 and the key 3 refers to the value 10. Now, if I go back and use empty brackets again, like this:

$a1[] = ‘next’;

The PHP engine looks at the highest integer key already being used, and sets the value ‘next’ to an integer key that’s one number higher. So, $a1 now holds the value ‘next’ at the key 4.

Now, if I want to do these steps in one line, the array function lets us do that. All I have to do is:

$a1 = array(5, 3=>10, ‘next’);

The values are added to an array using a comma-separated list. Also, notice the double-arrow. That’s how you specify the key for a particular value.

We can also use string keys. When you do that, you’re making an associative array. Performance-wise, associative arrays are just a little slower than arrays that use numeric indexes, but you can gain a lot when it comes time to access your values, since associative arrays allow you to assign meaningful names to your array’s keys.

Here’s an example of using a string as an array key:

$a2 = array(‘joe’=>’orange’, ‘anne’=>’peach’, ‘phil’=>’banana’);
$a2[‘sandy’] = ‘apple’;

When it comes time to access the values in an array, we turn to the square bracket syntax again:

echo $a2[‘anne’]; //prints out ‘peach’

As far as keys, only ints or strings can be used. Other data types won’t work. If you want to have an array inside an array, that’s easy to do:

$a3 = array(‘first’ => array(4, 5, ‘hello’=>’world’), ‘second’=>array($a1, $a2));

If I want to access one of the inner array’s values, I need to use more than one set of square brackets:

echo $a3[‘first’][‘hello’]; //prints out ‘world’

You can embed arrays as many layers deep as you want.

Another important point about PHP arrays is that PHP supports array addition. For example:

$a4 = $a1 + $a2;

$a4 becomes a combination of $array1 and $array2. When used this way, the plus sign is referred to as the array union operator. If 2 arrays have the same keys, whether integer keys or string keys, the latter array’s duplicate value won’t show up in the union of the two, only the first array’s value with that key will be present.

If you try to add an array and a non-array though, the PHP engine will throw a fatal error, because the addition operator requires that both the operands (the expressions on either side of the plus sign) be of the same supported type.

That’s enough information to get you started using arrays. Later, we’re going to take a look at the vast number of built-in PHP functions that you can use with your arrays.

February 06 2010 08:38 pm | Basics and PHP Programming Basics and PHP tutorial scripts

Comments are closed.