PHP Array

An array is used to store multiple values in one variable.

When To Use An Array

Arrays are used when you want to group many similar variables. For example, you may want to create a variable for a color. Instead of creating 5 different variables, each with its own color, you can create an array of colors.

$color[0] = “black”;
$color[1] = “white”;
$color[2] = “yellow”;
$color[3] = “blue”;
$color[4] = “red”;

We are using color as the name of this array. Then we have the keys, which are the numbers 1-5. You can create as many keys for a single array as you need. Each key equals a certain value that you can call somewhere else in your script. So, you can see that the formula for an array is:

$arrayname[key] = value;

To put the above array example to use, we could code something like this:

echo “My favorite color is ” . $color[0];
echo “<br />I also like ” . $color[2] . ” and ” . $color[4];

This bit of code would display:

My favorite color is black
I also like yellow and red

Notice we used the concentration operator, which is a period (.) to join multiple strings together.

Types Of Arrays

There are three types of arrays that you can create. These are:

  • Numeric array – An array with a numeric key
  • Associative array – An array where each key has its own specific value
  • Multidimensional array – An array which contains more than one array in itself

Numeric Array

The example above is a numeric array- each value is associated with a numerical key.

Associative Array

Using the same principle, we can create an associative array, which will assign a specific value to each key. To avoid becoming confused, keep in mind our formula mentioned above, which is:

$arrayname[key] = value;

An example of an associative array can look like:

$color["black"] = 5;
$color["white"] = 8;
$color["yellow"] = 2;
$color["blue"] = 10;
$color["red"] = 16;

To put the above associative array example to use, we could code something like this:

echo “I went to the store and bought ” . $color['white'] . ” gallons of white paint.<br />”;
echo “I also bought ” . $color['yellow'] . ” gallons of yellow paint, which was on sale.”;

This bit of code would display:

I went to the store and bought 8 gallons of white paint.
I also bought 2 gallons of yellow paint, which was on sale.

Multidimensional Array

Multidimensional arrays can become confusing, as they are more complex than numerical and associative arrays. Think of multidimensional arrays as a book. Each array is a chapter and a “subarray” is a page number within that chapter. Remember, a multidimensional array can contain more than one array within itself.

An example of a multidimensional array can look like:

$colors = array( array(“black”, gallons , quarts , liters),
array(“white”, gallons , liters),
array(“yellow”, gallons , quarts , liters)
);

To associate a multidimensional array as a book, the array “colors” is the name of the book. The arrays “black”, “white” and “yellow” are chapters in the book, and “gallons”, “quarts” and “liters” represent page numbers within each chapter.

Notice that each subarray can have its own values which do not have to be the same as other subarrays. Also, each value within a subarray ends with a comma except the last value within the subarray. This is important to keep in mind, otherwise this multidimensional array will not work.

To give each element of the multidimensional array a value to use, we would have something like this:

Array
(
[black] => Array
(
[0] => gallons
[1] => quarts
[2] => liters
)
[white] => Array
(
[0] => gallons
[1] => liters
)
[yellow] => Array
(
[0] => gallons
[1] => quarts
[2] => liters
)
)

To put the above multidimensional array example to use, we could code something like this:

echo “I have three ” . $colors['white'][1] . ” of white paint.<br />”;
echo “I also have eight ” . $colors['yellow'][0] . ” of yellow paint.”;

This bit of code would display:

I have three liters of white paint.
I also have eight gallons of yellow paint.

Arrays will make more sense to you and are used more when implementing them with for and while loops.