PHP Arrays

What is Array in PHP?

PHP Arrays. An array is a data structure that stores one or more similar type of values in a single value. For example, if you want to store 100 numbers then instead of defining 100 variables it’s easy to define an array of 100 lengths.


Why do you need to use an array?

An array is a data structure, which can store a fixed-size collection of elements of the same data type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Types Of Arrays in PHP?

In PHP, there are three types of arrays:

  1. Indexed arrays – Arrays with a numeric index
  2. Associative arrays – Arrays with named keys
  3. Multidimensional arrays – Arrays containing one or more arrays


1 Numerically Indexed Arrays

Let’s assume that you’ve been tasked with creating a simple website for a local office
supply company and you’re currently working on the section devoted to paper. One way
to manage the various items of stock in this category would be to place them in a numeric
array. You can see the simplest way of doing so in Example 6-1.

$paper[] = "Copier";
$paper[] = "Inkjet";
$paper[] = "Laser";
$paper[] = "Photo";

In this example, each time you assign a value to the array $paper, the first empty location within that array is used to store the value, and a pointer internal to PHP is incremented to point to the next free location, ready for future insertions. The familiar print_r function (which prints out the contents of a variable, array, or object) is used to verify that the array has been correctly populated. It prints out the following:

[0] => Copier
[1] => Inkjet
[2] => Laser
[3] => Photo


The previous code could also have been written as shown in Example 6-2, where the exact location of each item within the array is specified. But, as you can see, that approach requires extra typing and makes your code harder to maintain if you want to insert or remove supplies from the array. So unless you wish to specify a different order, it’s usually better to simply let PHP handle the actual location numbers.

Example 6-2. Adding items to an array using explicit locations

$paper[0] = "Copier";
$paper[1] = "Inkjet";
$paper[2] = "Laser";
$paper[3] = "Photo";


The output from these examples is identical, but you are not likely to use print_r in a
developed website, so Example 6-3 shows how you might print out the various types of
paper the website offers using a for a loop.

Example 6-3. Adding items to an array and retrieving them

$paper[] = "Copier";
$paper[] = "Inkjet";
$paper[] = "Laser";
$paper[] = "Photo";
for ($j = 0 ; $j < 4 ; ++$j)
echo "$j: $paper[$j]<br>";


This example prints out the following:

0: Copier
1: Inkjet
2: Laser
3: Photo

So far, you’ve seen a couple of ways in which you can add items to an array and one way
of referencing them, but PHP offers many more—which I’ll get to shortly. But first, we’ll
look at another type of array


<< Prev Next >>