PHP Arrays

What is an array?

An array is essentially a list of variables. Usually the elements in an array are all related; a common example of an array is the information a user has filled out on a web form. Each field is usually a single array element. A more simple example that we’ll work with here is a list of ice cream flavors.
Creating an array
Arrays are powerful and flexible. Arrays can be created in many different ways, but let’s start with a basic one:

1
2
3
4
$myArray = array();
$myArray[0] = ‘chocolate’;
$myArray[1] = ‘strawberry’;
$myArray[2] = ‘vanilla’;

We’ve just created an array with 3 elements. Each element has a key and a value: for example the first element has a key of 0 and a value of ‘chocolate.’

Accessing PHP arrays

If we reuse our array from above, we can access it without too much trouble:

1
2
3
4
5
6
7
$myArray = array();
$myArray[0] = ‘chocolate’;
$myArray[1] = ‘strawberry’;
$myArray[2] = ‘vanilla’;
echo ‘My favorite kind of ice cream is ‘ . $myArray[0];
echo ‘My second favorite kind of ice cream is ‘ . $myArray[1];
echo ‘My third favorite kind of ice cream is ‘ . $myArray[2];

This is perfectly valid PHP but it’s not terribly useful. Let’s move on to a more practical example.

Loops and php arrays

Here’s a more useful way to create an array:

1
2
3
4
$myArray = array();
for ( $n = 0; $n < 10; $n++) {
$myArray[$n] = $n .* 2 =. ($n * 2);
}

Why is this useful? Because now we can change our condition to be $n < 100 or $n < 10000 and our code will barely change. We have written scalable code, and scalable code is a good thing! Now we can access our array:

1
2
3
foreach ($myArray as $element) {
   echo $element;
}

Key-Value pair arrays

All php arrays are actually key-value pair arrays, but we typically ignore the key, as in the multiplication example above. Sometimes though, we want to maintain not just a list but a list of associated values. A good example of this is a phonebook:

1
2
3
4
5
6
7
8
$myArray = array();
$myArray[‘Jay’] =021 344 5555;
$myArray[‘Fred’] =021 344 6666;
$myArray[‘Betty’] =021 344 7777;
 
foreach ($myArray as $key =&gt; $value) {
   echo $key . “’s phone number is “ . $value;
}

See how our foreach statement refers to a $key and a $value? It’s referring to each row as a key-value pair. Compare this with the example in ‘Loops and arrays,’ which only makes the values available.

Other ways of creating php arrays

There is another way to create an array:

1
$myArray = array(‘chocolate’, ‘strawberry’, ‘vanilla’);

This is much more concise and more common in real scripts. It is equivalent to the first example in ‘Accessing an array,’ so for example you could use $myArray[0] and retrieve ‘chocolate.’

It is also common to create key-value pair arrays using this same kind of format, but with both the key and value supplied:

1
$myArray = array(‘scoop_1’ => ‘chocolate’, ‘scoop_2’ => ‘strawberry’, ‘cone_type’ => ‘vanilla’);

Arrays are one of the most fundamental features of any programming language. PHP has a great set of built-in functions for searching and sorting, as well as adding and removing elements from arrays.

This post is part of PHP First Steps Tutorial. Go on to the next session: Functions.

PHP arrays -24 hr tutorial Hey!
Based on the feedback from readers of this tutorial series I've expanded, revised, and improved this brief intro as a whole book. It is much longer, so you get a more in-depth guide to getting started. As a result you'll write better code. When you buy the book you also get a download password for all the source code from the tutorials. Build a functioning blog from scratch, and other useful mini-projects! Check it out »

No Comments

Be the first to start the conversation.

Leave a Reply

*

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>