TOC
Data types:

Working with arrays

Building lists of things is a very useful thing when programming. You may use it for a lot of different things, and in most programming languages, lists comes in different flavors. However, with PHP, you only have arrays. Fortunately, the language comes with lots of array related functions, allowing you to use them in all sorts of ways.

Arrays are pretty simple. It's like a list of items, where each entry has two properties: A key and a value. If you have the key, you may access a value directly - otherwise, you will have too look for it. This may seem a bit complicated in the beginning, but once you get the hang of it, you will realize that arrays are pretty simple to work with.

You can create an array using the array() function, like this:

<?php
$array = array();
?>

If you wish, you may send the desired new elements of the array along as a parameter, like this:

<?php
$animals = array("Monkey", "Lion", "Turtle", "Horse");
?>

In this example, we only specify the values. In a case like that, the key is a counted integer, starting at 0. That means that the first value will be given the key of 0, the next one will have the key of 1, and so on. Accessing a value can be done with this number, like this:

<?php
$animals = array("Monkey", "Lion", "Turtle", "Horse");
echo $animals[2];
?>

You simply specify the key of the desired element between the set of square brackets. In some cases, you may wish to start somewhere else than with a 0. That can be accomplished simply by setting the first key, like this:

<?php
$animals = array(2 => "Monkey", "Lion", "Turtle", "Horse");
echo $animals[2];
?>

The counting will then start with 2 instead. You may give all the items numbers your self, if you prefer that, as long as the keys are unique. In some cases, named keys will make more sense. No problem:

<?php
$namesAndAges = array("John Doe" => 45, "Jane Doe" => 33, "Dog Doe" => 11);
echo "The age of Jane Doe: " . $namesAndAges["Jane Doe"];
?>

We use the => operator to assign the age to the name, or in other words: The value to the key. In some cases, you may wish to add items later on, instead of when creating the array. Here is an example of that:

<?php
$names = array();
$names[] = "John Doe";
$names[] = "Jane Doe";
$names[] = "Dog Doe";
echo $names[1];
?>

We use an empty set of square brackets to make PHP create a key for the item, but obviously, you can give use the same techniques as above, when adding to the array.

Multidimensional arrays

The arrays in the examples above have one thing in common: They are all single dimensional, that is, they all consist of simple values. But what if you need an array that holds a range of arrays? Well, that's no problem at all, actually. PHP allows unlimited dimensions of arrays, which means that you can have an array that has a range of arrays, which have a range of arrays, and so on. Here is an example of that:

<?php
$contacts = array();

$contacts["Friends"] = array("Me", "John Doe");
$contacts["Family"] = array("Mom", "Dad");
$contacts["Enemies"] = array("Stalin", "Hitler");

foreach($contacts as $categoryName => $value)
{
    echo "<b>" . $categoryName . ":</b><br />";
    foreach($contacts[$categoryName] as $name)
    {
        echo $name . "<br />";
    }
    echo "<br />";
}
?>

Let's walk through this more complex example. We start out by creating an empty array called contacts. We wish to store names of contact persons in it, with different names in different categories. The first array will carry a set of category names, each of them pointing to an array with the persons who belong in that specific category. We assign both category names and contact persons on the fly - each category gets a newly created array of a couple of persons attached to it. To get a nice output of the list, we use two foreach loops - one that runs through the categories, and an inner loop which runs through the persons of the current category. We print out the name of the category, as well as the name of each person in it, followed by a linebreak. As you will see when you test this script, this creates a nice looking list of persons, divided in categories.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!