PHP Arrays

« Previous Chapter Next Chapter »

An array stores multiple values in one single variable.

What is an Array?

A variable is a storage area holding a number or text. The problem is, a variable will hold only one value.

An array is a special variable, which can store multiple values in one single variable.

If you have a list of items (a list of car names, for example), storing the players in single variables could look like this:

$player1="Sachin";
$player2="Maxwell";
$player3="Gilchrist";

However, what if you want to loop through the players and find a specific one? And what if you had not 3 players, but 300?

The best solution here is to use an array

An array can hold all your variable values under a single name. And you can access the values by referring to the array name.

Each element in the array has its own index so that it can be easily accessed.

In PHP, there are three kind of arrays:

  • Numeric array - An array with a numeric index
  • Associative array - An array where each ID key is associated with a value
  • Multidimensional array - An array containing one or more arrays

Numeric Arrays

A numeric array stores each array element with a numeric index.

There are two methods to create a numeric array.

1. In the following example the index are automatically assigned (the index starts at 0):

$players=array("Sachin","Maxwell","Gilchrist","Nadal");

2. In the following example we assign the index manually:

$players[0]="Sachin";
$players[1]="Maxwell";
$players[2]="Gilchrist";
$players[3]="Nadal";

Example

In the following example you access the variable values by referring to the array name and index:

<?php
$players[0]="Sachin";
$players[1]="Maxwell";
$players[2]="Gilchrist";
$players[3]="Nadal";
echo $players[0] . " and " . $players[1] . " are Cricket Players.";
?>

The code above will output:

Sachin and Maxwell are Cricket Players.


Associative Arrays

An associative array, each ID key is associated with a value.

When storing data about specific named values, a numerical array is not always the best way to do it.

With associative arrays we can use the values as keys and assign values to them.

Example

In this example we use an array to assign ages to the different persons:

$ages = array("William"=>32, "Boopathi"=>30, "Yousuf"=>34);

Example

This example is the same as Example, but shows a different way of creating the array:

$ages['William'] = "32";
$ages['Boopathi'] = "30";
$ages['Yousuf'] = "34";

The ID keys can be used in a script:

<?php
$ages['William'] = "32";
$ages['Boopathi'] = "30";
$ages['Yousuf'] = "34";

echo "William is " . $ages['William'] . " years old.";
?>

The code above will output:

William is 32 years old.


Multidimensional Arrays

In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.

Example

In this example we create a multidimensional array, with automatically assigned ID keys:

$families = array
  (
  "Dharsan"=>array
  (
  "William",
  "Adheem",
  "Dhoni"
  ),
  "Boopathi"=>array
  (
  "Glenn"
  ),
  "rachel"=>array
  (
  "Romain",
  "alice",
  "Malala"
  )
  );

The array above would look like this if written to the output:

Array
(
[Dharsan] => Array
  (
  [0] => William
  [1] => Adheem
  [2] => Dhoni
  )
[Boopathi] => Array
  (
  [0] => Glenn
  )
[rachel] => Array
  (
  [0] => Romain
  [1] => alice
  [2] => Malala
  )
)

Example

Lets try displaying a single value from the array above:

echo "Is " . $families['Dharsan'][2] .
" a part of the Dharsan family?";

The code above will output:

Is Dhoni a part of the Dharsan family?


Complete PHP Array Reference

For a complete reference of all array functions, go to our complete PHP Array Reference.

The reference contains a brief description, and examples of use, for each function


« Previous Chapter Next Chapter »

Have Any Suggestion? We Are Waiting To Hear from YOU!

Your Query was successfully sent!