PHP array

php array stores multiple values in one single variable.in php is actually an ordered map.

php arrays are lists of any kind of data, including other arrays.


what is an array in PHP

an array in php is a special variable, which can store more than one value at a time.

an array in php is actually an ordered map. a map is a type that associates values to keys.

   $array[0] ="steve jobs"; $array[1]="apple"; $array[2]="bill gate";

array index start from zero and end with total_length - 1 .


how create an array in PHP

in php array() method is used to create an array:

syntax:

         array();

php language provides three types of arrays:

  • indexed arrays - where each element is referenced by a numeric index.
  • associative arrays - array is also referred to as a hash or map.
  • multidimensional arrays - arrays containing one or more arrays.

PHP Indexed Arrays

these are arrays where each element is referenced by a numeric index, index start from 0.

each item in the array has a index a number, which can be used to retrieve an element from the array.

there are two ways to create indexed arrays:

the index can be assigned automatically:

     $items =  array("apple", "banana", "orange");

or

the index can be assigned manually:-

      $items[0] = "apple";  $items[1] = "banana";  $items[2]= "orange";

note : array elements can be accessed using the array[index] syntax.

   <?php
     
              $fruits =  array("apple", "banana", "orange", "graps");  
              
              echo  "<br/> i like   ". $fruits[0];
              
              echo  "<br/> i like   ". $fruits[1];
              
              echo  "<br/> i like   ". $fruits[2];
              
              echo  "<br/> i like   ". $fruits[3];
              
    ?>

accessing array elements:

  <?php     // how access array elements using for loop
     
              $fruits =  array("apple", "banana", "orange", "graps");  
          
              for( $i =0; $i < count($fruits) ; ++$i){
              
                                                           echo  "<br/> i like ". $fruits[ $i ];
                   
                                                         }
                 
                 // how access array elements using foreach loop
                 
               foreach( $fruits  as $value ){             
                                                           echo  "<br/> i like ". $value;
                  
                                                     }  
   ?>

PHP Associative Arrays

this type of array is also referred to as a hash or map. with associative arrays, each element is referenced by a string index.

there are two ways to create an associative array:

    $salary = array ("jobs" =>1000, "steve" => 2000, "bill gates" => 3000);
    $salary = ["jobs" =>1000, "steve" => 2000, "bill gates" => 3000];

or

    $salary ["jobs"] = 1000;

    $salary ["steve"] = 2000;
                     
    $salary ["bill gates"] = 3000;

note : array elements can be accessed using the array[key] syntax:

 <?php
    
         $salary = array("steve" => 1000, "jobs" => 2000, "bill gates" => 3000);
             
             echo  "<br/> steve salary". $salary["steve"];
             
             echo  "<br/> jobs salary". $salary["jobs"];
             
             echo  "<br/> bill salary". $salary["bill gates"]. "<br/>";
 ?>
 
 <?php        // how access associative array elements using foreach loop
    
             foreach( $salary  as  $key => $value ){
                                                                        
                                                                  echo "$key salary =  $value";
                                                                        
                                                                  echo "<br/>";
                                                                        
                                                         }   
 ?>

PHP - Array Length

the count() method is used to counts all elements in an array:

syntax:

         int    count( $array );
  <?php
            $arr = array(10,20,40,50,60,70);
            
            $array_length = count($arr);
            
         echo "total length of an array =". $array_length;
            
         echo "<br/> access last element of an array =". $arr[$array_length - 1 ];
  ?>

note : the actual values stored in array elements can be of any type, and you can mix types within a single array.

      $mix_data =  array(10, 20.30, true, "ram");