Topic outline

    • An array stores multiple values in one single variable. An array is a collection of data values organized as an ordered collection of key-value pairs. An array is a variable that stores a set or sequence of values. One array can have many elements, and each element can hold a single value, such as text or numbers, or another array. An array containing other arrays is known as a multidimensional array.

       In PHP, there are three kinds of arrays:  

      1.  Numeric array
      2. Associative array 
      3. Multidimensional array 


      1. NUMERIC ARRAY IN PHP 

      Numeric array is an array with a numeric index 

      To create numeric array, PHP Code is

      $products=array(‘read’,’expert’,’php’,’web’);

      The above code creates an array called $products containing the four values given are ‘read’,’expert’,’php’,’web’


      2. ACCESSING ARRAY CONTENTS

      To access the contents of a variable we use its name. If the variable is an arry ,you access the contents using both variable name and a key or index. The key or index indicates which of the values in the array we access.


      USING LOOPS TO ACCESS THE ARRAY

      The array is indexed by sequence of numbers we can use for loop to more easily display its contents

      For($i=0;$i<3;i++)

      {

      Echo $products[$i].” “;

      }


      USING sort()

      The following syntax illustrates sort() function results in the array being sorted into ascending order

      EXAMPLE : 1

      $products=array(‘tires’,’oil’,’spark’);

      Sort($products)


      EXAMPLE : 2

      $prices=array(100,10,4)

      Sort(prices)

      The prices will now appear in the order : 4,10,100


       NUMERIC ARRAY EXAMPLE 

      <html>   

      <body>   

       <?php      /* An array $flower_shop is created with three     Values - rose, daisy,orchid */     $flower_shop = array ( “rose","daisy","orchid"     );    

        /* Values of array $flower_shop is displayed based on index. The starting index of an array is Zero */     

      echo "Flowers: ".$flower_shop[0].",       ".$flower_shop[1].", ".$flower_shop[2]."";     ?>   </body>

       </html> 


      OUTPUT of the above given Example is as follows: 

      Flowers: rose, daisy, orchid  




      3. ASSOCIATIVE ARRAY IN PHP 

      Associative array is an array where each ID key is associated with a value 


      ASSOCIATIVE ARRAY EXAMPLE 

      <html>  

       <body>   

       <?php   /* Here rose, daisy and orchid indicates ID key and 5.00, 4.00, 2.00 indicates their values respectively */   

        $flower_shop = array ( "rose" => "5.00",   "daisy" => "4.00",   "orchid" => "2.00"  );   // Display the array values 

       echo "rose costs        .$flower_shop['rose'].",daisy costs       ".$flower_shop['daisy'].",and orchild        costs ".$flower_shop['orchild']."";     ?>   

      </body>

       </html> 


      OUTPUT of the above given Example is as follows: 

      rose costs 5.00,daisy costs 4.00,and orchild costs


      LOOP THROUGH AN ASSOCIATIVE ARRAY 

      <html>  

       <body>  

        <?php     $flower_shop=array("rose"=>"5.00","daisy"=>"4.00","orchid"=>"2.00"); 

         /* for each loop works only on arrays, and is used       to loop through each key/value pair in an array */  

         foreach($flower_shop as $x=>$x_value)

       {     

        echo "Flower=" . $x . , Value=" . $x_value;cho "<br>";   

       }    

       ?>    

      </body>

       </html> 


      OUTPUT OF THE ABOVE GIVEN EXAMPLE IS AS FOLLOWS: 

      Flower =rose, Value = 5.00 Flower=daisy, Value =4.00 Flower=orchid, Value =2.00


      MULTIDIMENSIONAL ARRAY IN PHP 

      Multidimensional array is an array containing one or more arrays 

      MULTIDIMENSIONAL ARRAY EXAMPLE 

      <html>  

      <body> 

        <?php   

         /* Here $flower_shop is an array, where rose, daisy and orchid   are the ID key which indicates rows and points to array which   have column values. 

      $flower_shop = array("rose" => array( "5.00", "7 items", "red" ), daisy" => array( "4.00", "3 items", "blue" ),       "orchid" => array( "2.00", "1 item", "white" ),  );   /* in the array $flower_shop['rose'][0], ‘rose’ indicates row and  ‘0’ indicates column */    echo "rose costs ".$flower_shop['rose'][0].  ", and you  get ".$flower_shop['rose'][1].".<br>";    

         echo "daisy costs ".$flower_shop['daisy'][0].     ", and you get bflower_shop['daisy'][1].".<br>";    

         echo "orchid costs ".$flower_shop['orchid'][0].     ", and you get ".$flower_shop['orchid'][1].".<br>";    ?>  

      </body> 

      </html> 


      OUTPUT OF THE ABOVE GIVEN EXAMPLE IS AS FOLLOWS: 

      rose costs 5.00, and you get 7 items. daisy costs 4.00, and you get 3 items. orchid costs 2.00, and you get 1 item.