An array in PHP is actually an ordered map. A map is a type that associates values to keys.
The array is used to store one or more similar type of values in a single value.
array( key => value, key2 => value2, key3 => value3, ... )
Each map is a key-value
pair.
The key
as also known as array index
.
The key
can either be an integer
or a string
.
The value
can be of any type.
<?php $fruits = array(1=>"Apple", 2=>"Orange", 3=>"Pear"); // the keys are integers (also known as Indexed Arrays) $colors = array( // the keys are strings (also known as Associative Arrays) "Red" => "#F00", "Green" => "#0F0", "Blue" => "#00F", ); ?>
short array syntax (php5.4+)
short array syntax replaces array() with [].
<?php $fruits = array("Apple", "Orange", "Pear"); $fruits = ["Apple", "Orange", "Pear"]; ?>
The comma after the last array element is optional and can be omitted.
<?php array(1, 2, ); // The comma is usually omitted for single-line arrays array(1, 2); $colors = array( // The comma is usually used for easier addition of new elements at the end "Red" => "#F00", "Green" => "#0F0", "Blue" => "#00F", ); $colors = array( "Red" => "#F00", "Green" => "#0F0", "Blue" => "#00F", "Cyan" => "#00ffff", ); ?>
Array elements can be accessed using the array[key]
syntax.
<?php $fruits = array(1=>"Apple", 2=>"Orange", 3=>"Pear"); $colors = array( "Red" => "#F00", "Green" => "#0F0", "Blue" => "#00F", ); var_dump($fruits[2]); var_dump($colors["Blue"]); ?>
Output:
string(6) "Orange" string(4) "#00F"
An existing array can be modified by assigning values to the array.
$arr[key] = value;
$arr[] = value;
<?php $arr = array( 1, 8 => 2); $arr[0] = 6; // Change the first element value to 6 $arr[] = 3; // This is the same as $arr[9] = 3; // at this point of the script var_dump($arr);
Output;
array(3) { [0]=> int(6) [8]=> int(2) [9]=> int(3) }
To remove a key/value pair, call the unset() function on it.
Be aware that the array will not be reindexed.
<?php $array = array(1, 2, 3, 4, 5); print_r($array); unset($array[3]); // This removes the fourth element from the array print_r($array); unset($array); // This deletes the whole array print_r($array); ?>
Output:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) Array ( [0] => 1 [1] => 2 [2] => 3 [4] => 5 ) <br /> <b>Notice</b>: Undefined variable: array in <b>D:\Apache24\htdocs\index.php</b> on line <b>9</b><br />
Use var_dump()
and print_r()
to print human-readable information about array.
$fruits = array(1=>"Apple", 2=>"Orange", 3=>"Pear"); // the keys are integers var_dump($fruits); echo "<br>"; $colors = array( // the keys are strings "Red" => "#F00", "Green" => "#0F0", "Blue" => "#00F" ); print_r($colors);
<?php $array = array( 12 => "a", // 12, Canonical Syntax, an integer -12 => "b", // -12, Canonical Syntax, key could be a negative integer "foo" => "c",// "foo", Canonical Syntax, a string "3" => "d", // 3, Decimal integers string,will be cast to the integer type "03" => "e", // "03", Not valid decimal integer "+3" => "f", // "+3", the key is string type "3C" => "g", // "3C", the key is string type 6.8 => "h", // 6, the fractional part will be truncated true => "i", // 1, be cast to the integers.true for 1, false for 0 null => "j", // "", Null will be cast to the empty string. // Array() => "k", Arrays can not be used as keys,Illegal offset type. // (New obj) => "l", Objects can not be used as keys,Illegal offset type. ); var_dump($array); ?>
Each array index
is unique and references a corresponding value.
If there are same keys, only the last one will be used. all others are overwritten.
<?php $array = array( 1 => "a", 1 => "b", "1" => "c", true => "d", ); var_dump($array); ?>
Output:
array(1) { [1]=> string(1) "d" }
The key is optional.
the indexes are automatically assigned and start with 0.
<?php $fruits = array("Apple", "Orange", "Pear"); var_dump($fruits); ?>
Output:
array(3) { [0]=> string(5) "Apple" [1]=> string(6) "Orange" [2]=> string(4) "Pear" }
If the key is not specified, PHP will use the increment of the largest previously used integer key.
<?php $fruits = array( "Apple", "Orange", 5=>"Pear", "Banana", ); var_dump($fruits); ?>
Output:
array(4) { [0]=> string(5) "Apple" [1]=> string(6) "Orange" [5]=> string(4) "Pear" [6]=> string(6) "Banana" }
<?php $array = array( "foo" => "Apple", 3 => 90, ); var_dump($array); ?>
array(4) { ["foo"]=> string(5) "Apple" [3]=> int(90) }
The array's element can be another array.
$transcripts = array( "bob" => array( "Mathematics" => "B", "Management" => "A", ), "jack" => array( "Mathematics" => "A", "Management" => "A+", ), "dennis" => array( "Mathematics" => "C", "Management" => "C+", ), );
Values in the multi-dimensional array are accessed using multiple index.
/* Accessing multi-dimensional array values */ echo $transcripts['bob']['Management']; // Output: A