Arrays

Share this post on:

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

Advantage of PHP Array

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

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

Sorting: We can sort the elements of the array.

Create an Array in PHP

In PHP, the array() function is used to create an array.

array();
Example
<?php
$animal = array("Cat", "Dog", "Fish");
echo "I Love " . $animal[0] . ", " . $animal[1] . " and " . $animal[2] . ".";
?>
Output
I Love Cat, Dog and Fish.

In PHP, there are three types of arrays:

  • Indexed arrays – Arrays with a numeric index
  • Associative arrays – Arrays with named keys
  • Multidimensional arrays – Arrays containing one or more arrays

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:

<?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.
2nd way:
?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]";  
?>
Output
Season are: summer, winter, spring and autumn

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

<?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
2nd way
<?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

PHP Multidimensional Array

PHP multidimensional array is also known as array of arrays. It allows you to store tabular data in an array. PHP multidimensional array can be represented in the form of a matrix which is represented by row * column.

Example
<?php    
$emp = array  
  (  
  array(1,"sonoo",400000),  
  array(2,"john",500000),  
  array(3,"rahul",300000)  
  );  
  
for ($row = 0; $row < 3; $row++) {  
  for ($col = 0; $col < 3; $col++) {  
    echo $emp[$row][$col]."  ";  
  }  
  echo "<br/>";  
}  
?>
Output
1 sonoo 400000 
2 john 500000 
3 rahul 300000
Share this post on:

Leave a Reply