Login | Contact Us | Site Map | Resources

PHP Arrays

Home > PHP Tutorial > Creating An Array

Creating An Array

Arrays come in three different varieties: numerical, associative and multidimensional. Although they are similar, we will discuss each one separately to avoid confusion.

Numerical Arrays

By default, arrays are numerical, meaning that each value stored in an array is represented by a number. The number index always begins at 0 instead of 1.

Creating a numerical array is very simple. You can assign each value manually, or use the array() function, which accepts multiple values and associates each value with a unique ID number, or numerical key.

These three examples will each create an identical array:

  $animals[0] = "Monkey";
  $animals[1] = "Panda";
  $animals[2] = "Spider";
  $animals[3] = "Elephant";
  $animals[4] = "Ferret";

  $animals = array("Monkey", "Panda", "Spider", "Elephant", "Ferret");

  $animals = array(1 => "Monkey", 2 => "Panda", 3 => "Spider", 4 => "Elephant", 5 => "Ferret");

The print_r() function will give you readable information about any variable that it is given, and so it is useful if you want to view information about an array.

  $animals = array("Monkey", "Panda", "Spider", "Elephant", "Ferret");
  echo "Result: ";

Result: Array ( [0] => Monkey [1] => Panda [2] => Spider [3] => Elephant [4] => Ferret )

Using each value assigned to an array is as simple as knowing which key the value was assigned to.

  $answer = array("lizard", "panda", "mouse", "snake", "cat");
  echo "What's the definition of a narrow squeak?";
  echo "A thin " . $answer[2] . "!";

You have to figure out the code in order to get the joke!

Values can be added to the end of a numerical array at any time using the following syntax: $array_name[] = 'new value';

Associative Arrays

Associative arrays allow you to us a value as a key, so that a value can be assigned to each value.

Let's say, for example, that you run a zoo, and that you have a list of animals living at your zoo, and you need to keep track of how many of each animal that you have living at your zoo. This is too much information for a numerical array, but not for an associative array.

Creating an associative array is very simple. You have to assign each key and value manually, but there are two methods that you can use.

  $zoo_animals['Monkey'] = 15;
  $zoo_animals['Panda'] = 3;
  $zoo_animals['Spider'] = 167;
  $zoo_animals['Elephant'] = 5;
  $zoo_animals['Ferret'] = 7;

  $zoo_animals = array("Monkey" => 15, "Panda" => 3, "Spider" => 167, "Elephant" => 5, "Ferret" => 7);

As with numerical arrays, the print_r() function can be used to view the array information, and echo can be used to output each key's value.

  $zoo_animals = array("Monkey" => 15, "Panda" => 3, "Spider" => 167, "Elephant" => 5, "Ferret" => 7);

  echo "Our zoo has" . $zoo_animals['Monkey'] . " monkeys!";
  echo "Our zoo has " . $zoo_animals['Panda'] . " pandas!";
  echo "Our zoo has " . $zoo_animals['Elephant'] . " elephants!";
  echo "Our zoo has " . $zoo_animals['Ferret'] . " ferrets!";
  echo "Our zoo has about " . $zoo_animals['Spider'] . " spiders, none of which are in cages!";

Values can be added to the end of an associative array at any time using the following syntax: $array_name['key'] = 'value';

Multidimensional Arrays

Multidimensional arrays are arrays containing one or more other arrays. Besides being confusing, they are not often used, but we will cover a quick example so that you know how that they can work.

  $animals = array("Pets"=>array("dog", "cat", "hamster"), "Farm Animals"=>array("Horse", "Cow", "Pig"), "Wildlife"=>array("Elephant", "Deer", "Camel"));

In the example we have an array called "animals" which contains three different arrays ("Pets", "Farm Animals" & "Wildlife"), each of which contain three array values of their own. The "animals" array is an associative array, but each of the three other arrays are numerical arrays, meaning that the following example will echo "Horse":

  $animals = array(
    "Pets"=>array("dog", "cat", "hamster"),
    "Farm Animals"=>array("Horse", "Cow", "Pig"),
    "Wildlife"=>array("Elephant", "Deer", "Camel")

  echo $animals['Farm Animals'][0];