Array in PHP


Array is a variable that is used to store multiple value in a single variable.
Values are stored in the form of key-value pair. Individual elements of an array can be identify using its key along with name of the array.

Types of Array


There are two types of Array in PHP:
(1) Numeric Array
(2) Associative Array

     

Numeric Array


In numeric array each element having Numeric key associated with it.
Key value always starts with 0. So the key value of first element of an array is 0, Second element is 1 and so on.
array () function is used to create Numeric Array. 

Syntax
$ArrayName = array (Value1, Value2… ValueN);

In numeric array each elements are assigned a numeric key value starting from 0 for first element and so on.

Example
<?php
    $MyArray = array("A","B","C");
    print_r ($MyArray);
?>
Output
Array ( [0] => A [1] => B [2] => C )

Individual element of an array can be referred in PHP script using its key value. 

Example 
<?php
   $MyArray = array("A","B","C"); 
   echo $MyArray[1];
?>
Output
B

In Numeric array for loop, while loop or do while loop can be used to iterate through each element because in numeric array key values are consecutive and Numeric starting from 0. 

Example
<?php
   $MyArray = array("Sachin","Ponting","Gilchrist");
   for ($i=0; $i < 3; $i++)
   echo $MyArray[$i]." ";
?> 
Output
Sachin Ponting Gilchrist

Associative Array


In Associative array each element having key associated with it. The key can be either numeric or string.
array () function is used to create an associative array. 

Syntax
$ArrayName = array (Key1=>Value1, Key2=>Value2… KeyN=>ValueN);

If  key  is not specified for each element in the array then PHP will automatically assign them Key starting from 0 and creates a Numeric array.

Example
<?php
$MyArray = array("A","B","C");
print_r ($MyArray);
?>
Output
Array ( [0] => A [1] => B [2] => C ) 

In order to create an associative array key must be specified for each element of an array explicitly.
Key can be either numeric or string.

Example (Numeric Key Value)
<?php
$MyArray = Array([10] => A [20]=>B [30]=> C);
print_r($MyArray);
?>
Output
Array ( [10] => A [20] => B [30] => C )

Example (String Key Value)
<?php
$MyArray = Array("RAJU"=>1000 "MANU"=>2000 "VIPU"=>300);
print_r($MyArray);
?>
Output
Array ( [RAJU] => 1000 [MANU] => 2000 [VIPU] => 3000 )

Individual element of an array can be referred in PHP script using its key value.

Example
<?php
$MyArray = Array("RAJU"=>1000 "MANU"=>2000 "VIPU"=>300);
echo "Score of RAJU is". $MyArray["RAJU"];
?>
Output
Score of RAJU is 1000

In Associative array for loop, while loop or do while loop can not be used to iterate through each element because in Associative array key values are not consecutive.
foreach looping structure is used to iterate throgh each element in Associative array.

Example
<?php
$MyArray = Array("RAJU"=>1000 "MANU"=>2000 "VIPU"=>300);
foreach ($MyArray as $Item)
echo $Item." ";
?> 
Output
1000 2000 300
 

Download Projects


Download Programs