PHP Arrays

admin_img Posted By Bajarangi soft , Posted On 05-10-2022

An array is a special variable that we use to store or hold more than one value in a single variable without having to create more variables to store those values.

Types of Array in PHP

Introduction of Array

We used array to hold multiple values of similar type in a single variable.
For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length.

Syntax :

array( ) ;

Types of arrays in PHP :
  • Indexed Array 
  • Assosiative Array 
  • Multidimentional Array 

Indexed /Numeric Array

These arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero.

First method of create array :

$array = array( a, b, c, d, e) ;


Second method of create array :

         $array[0] = "one";
         $array[1] = "two";
         $array[2] = "three";
         $array[3] = "four";
         $array[4] = "five";


Example 1 :

  <?php
       $numbers = array( 1, 2, 3, 4, 5);
         
         foreach( $numbers as $value ) {
            echo "$value </br>";
         }        
      ?>

Output :

1
2
3
4
5


Example 2 :

<?php
         $numbers[0] = "one";
         $numbers[1] = "two";
         $numbers[2] = "three";
         $numbers[3] = "four";
         $numbers[4] = "five";
         
        foreach( $numbers as $value ) {
            echo "$value <br />";
         }
?>


Output :

one
two
three
four
five

Assosiative Array

 Associative array will have their index as string so that you can establish a strong association between key and values.


First method of create array :

$salary = array("mike" => "10000", "dustin" => "20000", "willy" => "3", ) ;


Second method of create array :

         $array['mike'] = "10000";
         $array['dustin'] = "20000";
         $array['willy'] = "30000";

Example 1 :

<?php<?php
$salary = array("mike"=>"35000", "dustin"=>"37000", "willy"=>"43000");

foreach($salary as $key => $value) {
  echo  "". $key . " => " . $value;
  echo "<br>";
}
?>

Output :

mike => 35000
dustin => 37000
willy => 43000


Example 2 :

<?php
$salary["mike"]="350000";    
$salary["dustin"]="450000";    
$salary["sink"]="200000";    
echo "mike salary: ".$salary["mike"]."<br/>";  
echo "dustin salary: ".$salary["dustin"]."<br/>";  
echo "sink salary: ".$salary["sink"]."<br/>";  
?>

Output :

mike salary: 350000
dustin salary: 450000
sink salary: 200000

Multidimentional Array 

A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index.  

Example :

<?php    
$emp = array  
  (  
  array(1,"mike",400000),  
  array(2,"dustin",500000),  
  array(3,"dude",300000)  
  );  
  
for ($row = 0; $row < 3; $row++) {  
  for ($col = 0; $col < 3; $col++) {  
    echo $emp[$row][$col]."  ";  
  }  
  echo "<br/>";  
}  
?>

Output :

1 mike 400000
2 dustin 500000
3 dude 300000

Related Post