PHP Associative Array

PHP allows you to associate name/label with each array elements in PHP using => symbol. Such way, you can easily remember the element because each element is represented by label than an incremented number.

Definition

There are two ways to define associative array:

1st way:

  1. $salary=array(“Sonoo”=>”550000″,”Vimal”=>”250000″,”Ratan”=>”200000”);  

2nd way:

  1. $salary[“Sonoo”]=”550000″;  
  2. $salary[“Vimal”]=”250000″;  
  3. $salary[“Ratan”]=”200000″;  

Example

  

<?php    
$salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");  
echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";  
echo "Vimal salary: ".$salary["Vimal"]."<br/>";  
echo "Ratan salary: ".$salary["Ratan"]."<br/>";  
?>  

Output:

Sonoo salary: 550000
Vimal salary: 250000
Ratan salary: 200000

   

<?php    
$salary["Sonoo"]="550000";  
$salary["Vimal"]="250000";  
$salary["Ratan"]="200000";   
echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";  
echo "Vimal salary: ".$salary["Vimal"]."<br/>";  
echo "Ratan salary: ".$salary["Ratan"]."<br/>";  
?> 

Output:

Sonoo salary: 550000
Vimal salary: 250000
Ratan salary: 200000

Traversing PHP Associative Array

By the help of PHP for each loop, we can easily traverse the elements of PHP associative array.

   

<?php    
$salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");  
foreach($salary as $k => $v) {  
echo "Key: ".$k." Value: ".$v."<br/>";  
}  
?> 

Output:

Key: Sonoo Value: 550000
Key: Vimal Value: 250000
Key: Ratan Value: 200000

PHP Arrays

PHP array is an ordered map (contains value on the basis of key). It is used to hold multiple values of similar type in a single variable.


Advantage of PHP Array

Less Code: We don’t need to define multiple variables.

Easy to traverse: By the help of single loop, we can traverse all the elements of an array.

Sorting: We can sort the elements of array.


PHP Array Types

There are 3 types of array in PHP.

  1. Indexed Array
  2. Associative Array
  3. Multidimensional Array

PHP Indexed Array

PHP index is represented by number which starts from 0. We can store number, string and object in the PHP array. All PHP array elements are assigned to an index number by default.

There are two ways to define indexed array:

1st way:

  1. $season=array(“summer”,”winter”,”spring”,”autumn”);  

2nd way:

$season[0]="summer";  
$season[1]="winter";  
$season[2]="spring";  
$season[3]="autumn";  

Example

<?php  
$season=array("summer","winter","spring","autumn");  
echo "Season are: $season[0], $season[1], $season[2] and $season[3]";  
?>  

Output:

Season are: summer, winter, spring and autumn

<?php  
$season[0]="summer";  
$season[1]="winter";  
$season[2]="spring";  
$season[3]="autumn";  
echo "Season are: $season[0], $season[1], $season[2] and $season[3]";  
?>  

PHP Associative Array

We can associate name with each array elements in PHP using => symbol.

There are two ways to define associative array:

1st way:

  1. $salary=array(“Sonoo”=>”350000″,”John”=>”450000″,”Kartik”=>”200000”);  

2nd way:

$salary["Sonoo"]="350000";  
$salary["John"]="450000";  
$salary["Kartik"]="200000";  

Example

<?php    
$salary=array("Sonoo"=>"350000","John"=>"450000","Kartik"=>"200000");    
echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";  
echo "John salary: ".$salary["John"]."<br/>";  
echo "Kartik salary: ".$salary["Kartik"]."<br/>";  
?>    

Output:

Sonoo salary: 350000
John salary: 450000
Kartik salary: 200000

 

<?php    
$salary["Sonoo"]="350000";    
$salary["John"]="450000";    
$salary["Kartik"]="200000";    
echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";  
echo "John salary: ".$salary["John"]."<br/>";  
echo "Kartik salary: ".$salary["Kartik"]."<br/>";  
?>   

Output:

Sonoo salary: 350000
John salary: 450000
Kartik salary: 200000