Arrays in PHP – Single and Multi Dimensions

PHP

If you want to store multiple values to a single variable name then you have to use Arrays. Arrays use a single variable name with multiple keys. Those keys hold the values or actually map them. Arrays are quite useful for storing and retrieving multiple values, and for storing data obtained through MySQL operations.

To create an Array in PHP, we use the array() function. The elements of an array are generally placed inside a paid of round brackets () but curly ones work as well{}.

Indexed Arrays

These have numbers as their keys.

Example:

<?php
$names = array ("Ashish","Michael","Chris");
?>

The above values are stored as:

$names[0]="Ashish";
$names[1]="Michael";
$names[2]="Chris";
Note: The number within the square brackets are called keys and they start from 0.

Associative arrays

These types of arrays use named keys instead of numbers. The => assignment operator is usually used for creating and storing values into associative arrays.

For example:

<?php
$names = array ("Ashish"=>"Kathmandu","Michael"=>"New York","Chris"=>"Australia");
//Or you can just use square braces PHP 5.4 onwards
$names = array ["Ashish"=>"Kathmandu","Michael"=>"New York","Chris"=>"Australia"];
//Doesn't matter how you format it
$names = array (
"Ashish"=>"Kathmandu",
"Michael"=>"New York",
"Chris"=>"Australia"
);
?>

The above values are stored as:

$names["Ashish"] = "Kathmandu";
$names["Michael"] = "New York";
$names["Chris"] = "Australia";
To dump the contents of an array, use the print_r or var_dump function.

For the above $names array, use the following method to view all the values within the array.

<?php
$names = array (
"Ashish"=>"Kathmandu",
"Michael"=>"New York",
"Chris"=>"Australia"
);
print_r($names);
echo "<br />";
var_dump($names);
?>
Output:
Array ( [Ashish] => Kathmandu [Michael] => New York [Chris] => Australia )

array(3) { ["Ashish"]=> string(9) "Kathmandu" ["Michael"]=> string(8) "New York" ["Chris"]=> string(9) "Australia" }
Note: print_r() and var_dump() are used for developmental purposes only.

Multi Dimensional Arrays

These arrays take it to the next level with two or more keys. They are multiple levels deep. Think of them as arrays within another array.

Example:

<?php
$names = array(
array("Ashish",27),
array("Michael",20),
array("Chris",35),
);
print_r($names);
?>
Output:
Array ( [0] => Array ( [0] => Ashish [1] => 27 ) [1] => Array ( [0] => Michael [1] => 20 ) [2] => Array ( [0] => Chris [1] => 35 ) )

The values are stored as:

$names[0][0] = "Ashish";
$names[0][1] = "27";

$names[1][0] = "Michael";
$names[1][1] = "20";

$names[2][0] = "Chris";
$names[2][1] = "35";

Try printing it out the multi-dimensional array we created this way

<?php
$names = array(
array("Ashish",27),
array("Michael",20),
array("Chris",35),
);
echo $names[1][0];
echo "<br />";
echo $names[1][1];
?>
Output:
Michael
20

Let us try a three level multi dimensional array

<?php
$names = array(
"Level1"=>array(
"Level2"=>array(
"Level3"=>"Ashish"
)
)
);
var_dump($names);
?>
Output:
Array ( [Level1] => Array ( [Level2] => Array ( [Level3] => Ashish ) ) )

This forms an array as:

$names["Level1"]["Level2"]["Level3"] = "Ashish";

Other Functions Related to Arrays

Use array_push() to add values at the end of an array. The function unset() destroys the values. Finally join() or implode() and explode() to join and unjoin values separated by glue strings.