w3resource

PHP Data Types : Arrays

Description

An array in PHP is a collection of key/value pairs. This means that it maps values to keys. Array keys (or indexes) may be either integers or string whereas values can be any type.

Array() construct

An array can be declared using the array() language construct, which generally takes the following format.

array( key1=> value1, 
key2=> value3, 
key3=> value3, 
.......... )

Key1, key2, key3 may be an integer or string.

value1, value2, value3 may be any value of any type.

As of PHP 5.4 a short array syntax [] is used instead of array().

Example:

<?php
$fruits = array(
fruit1 => "Banana",
fruit2 => "Apple" 
);
// declaring the above array as of PHP 5.4
$fruits = [
fruit1 => "Banana",
fruit2 => "Apple" 
];
?>

In the above example, Banana and Apple are the values and fruit1, fruit2 are the keys of the array $fruits.

Indexed and Associative Arrays

In PHP there is two kinds of arrays : indexed array and associative array. The only difference is that numeric values are used as 'keys' in indexed array start from zero (0) and in associative array, strings are used as 'keys'. PHP does not differentiate between indexed and associative arrays, therefore a PHP array may contain strings as well as integers as 'keys'.

Example : Indexed arrays with key

<?php
$fruits[0]="Banana";
$fruits[1]="Apple";
$fruits[2]="Mango";
$fruits[3]="Coconut";  
var_dump($fruits);
?>

Output :

 array(4) { [0]=> string(6) "Banana" [1]=> string(5) "Apple" [2]=> string(5) "Mango" [3]=> string(7) "Coconut" }

Here var_dump() function is used to display structured information of an array.

Example : Indexed arrays without key

<?php
$fruits = array("Banana", "Apple", "Mango", "Coconut");
var_dump($fruits);
?>

Output :

 array(4) { [0]=> string(6) "Banana" [1]=> string(5) "Apple" [2]=> string(5) "Mango" [3]=> string(7) "Coconut" }

In PHP array key is optional. If no key is specified, keys start from zero (0).

Example : Integer and string keys together

<?php
$fruits=array(
0 => "Banana",
"fruit1" => "Apple",
11 => "Mango",
-34 => "Coconut",
);
var_dump($fruits);
?>

Output :

 array(4) { [0]=> string(7) "Banana" ["fruit1"]=> string(5) "Apple" [11]=> string(5) "Mango" [-34]=> string(7) "Coconut" } 

Example: Keys are not present on all elements

<?php
$fruits=array(
"Banana",
11=>"Apple",
"Mango",
"fruit1" => "Coconut",
);
var_dump($fruits);
?>

Output :

 array(4) { [0]=> string(6) "Banana" [11]=> string(5) "Apple" [12]=> string(5) "Mango" ["fruit1"]=> string(7) "Coconut" }

In the above example the third value " Mango " is assigned the key 12 because the largest integer key before that was 11.

Storing Data in an Array

Storing a value in an array is easy, you can use any of the following method to store date:

<?php
//Store data through an Indexed array.
$country[0] = 'India'; 
$country[1] = 'USA'; 
$country[2] = 'Peru';
//Store data through an associative array.
$price['country1'] = 'India';
$price['country2'] = 'USA';
$price['country3'] = 'Peru';
?>

Accessing array elements

The elements of an array can be accessed using the array[key] syntax, see the following example.

<?php
$fruits=array(
0 => "Banana",
"fruit1" => "Apple",
11 => "Mango",
-34 => "Coconut",
);
echo($fruits[0]);
echo($fruits["fruit1"]);
echo($fruits[11]);
echo($fruits[-34]);
?>

Output :

 BananaAppleMangoCoconut

Multidimensional array

A multidimensional array is a structure which holds various arrays in an array.

Here is an example.

<?php
$class = array
(
"ClassV" => array
(
"David", 
"Alex",
"Emma"
),
"ClassVI" => array
(
"Tyler",
"Ryan",
"Killian"
),
"ClassVII" => array
(
"Liliana",
"Dante",
"Zaira"
)
);
?>

Accessing multidimensional array elements

<?php
$class = array
("ClassV" => array
("David", 
"Alex",
"Emma"
),
"ClassVI" => array
("Tyler",
"Ryan",
"Killian"
)
);
// Print the second name of the student from class V.
echo($class["ClassV"][1]);
// Output - Alex
?>

Previous: Strings
Next: Objects



Follow us on Facebook and Twitter for latest update.