Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday, 12 May 2016

PHP 5 ARRAYS

PHP 5 Arrays

An array is a Variable which can stores multiple values in one single variable:

In PHP, there are three types of Array:-
1) Indexed Array (Array with a numeric index).
2) Associate Array (Array with Named Keys).
3) multidimensional Array (Array containing one or more array).

                                          INDEXED ARRAY


Syntax:-
    Case1:    $Array=array("aaa","bbb");
                            OR
    Case2:    $Array[0] = "aaa";
                  $Array[1] = "bbb";
Example:-

       <?php

        $Fruit=array("Apple", "Banana", "Mango");

        echo "I like ".$Fruit[0].", ".$Fruit[1].", ".$Fruit[2];

        ?>

OTPUT:-

       I like Apple, Banana, Mango

* Where [0],[1] and [2]... is Index of the array. Bydefault Index is start with  0.


                                                    ASSOCIATIVE ARRAY


 In this, two parameter passes i.e. one is "key" and other is "value".


Syntax:- 




Example:-

        <?php
         $Sharma=123;                
       $Array=array("Gopal"=>$Sharma,"Megha"=>'Parkar',  
      "Akshay"=>789);
       print_r($Array);
         ?>
OTPUT:-

      Array ( [Gopal] => 123 [Megha] => Parkar [Akshay] => 789 )          


* Where "Gopal","Megha","Akshay" are KEY and "123","Parkar","789" are VALUE for the particular KEY.



                                    MULTIDIMENSIONAL ARRAY


It is the part of the PHP-Advance, in which Array have more than one Row and Column.


Example:- Suppose you want to store Student Name, Student Age, Student Roll Number.

         <?php
                    $Student = array
          (
               array("Gopal",21,11068),
               array("Shayam",23,11069),
               array("Akshay",22,11071),
               array("Tuneer",25,11123)
           );

/*Now the two-dimensional $Student array contains four arrays, and it has two indices: row and column.
To get access to the elements of the $Student array we must point to the two indices (row and column):  */
          echo $Student[0][0].": Age: ".$Student[0][1].", Roll No: ".$Student[0]             [2].".<br>";
          echo $Student[1][0].": Age: ".$Student[1][1].", Roll No: ".$Student[1]             [2].".<br>";
          echo $Student[2][0].": Age: ".$Student[2][1].", Roll No: ".$Student[2]             [2].".<br>";
          echo $Student[3][0].": Age: ".$Student[3][1].", Roll No: ".$Student[3]             [2].".<br>";
        ?>OTPUT:-
          Gopal: Age: 21, Roll No: 11068.
          Shayam: Age: 23, Roll No: 11069.
          Akshay: Age: 22, Roll No: 11071.
          Tuneer: Age: 25, Roll No: 11123.



* Where First Index is ROW while the Second Index is Column.


                                  

                                     COUNT FUNCTION

THE COUNT FUNCTION IS USED TO COUNT THE LENGTH OF THE ARRAY and give an integer answer.


Example:- 

           <?php 
             $Array=array("RAM","Shyam","Aman","Yaman");
             echo count($Array);
           ?>
OUTPUT:-
           4


No comments:

Post a Comment