Friday, 14 August 2015

PHP Arrays:-
  • An array is a data structure that stores one or more similar type of values(homogeneous) in a single value.
  • If we want to store 10 numbers then instead of defining 10 variables, its easy to define an array of 10 length.
  • There are three different kind of arrays and each array value is accessed using an ID which is called array index.
    • Numeric Array :-
      • It can be stated as an array with a numeric index.
      • Values are stored and accessed in linear fashion.
      • These arrays can store numbers, strings and any object but their index will be presented by numbers. By default array index starts from zero.
      • It can be explained further with a suitable example.
                              numeric array.php:-
                              <html>
                              <body>
                              <?php

                              echo "<b><i>First method to create array(array with integer)</i></b>";
                              echo "<br/><br/>";
                              $numbers = array( 1, 2, 3, 4, 5);
                              echo "Array's Data is <b> $numbers[0] </b> <br/>";
                              echo "Array's Data is <b> $numbers[1] </b> <br/>";
                              echo "Array's Data is <b> $numbers[2] </b> <br/>";
                              echo "Array's Data is <b> $numbers[3] </b> <br/>";
                              echo "Array's Data is <b> $numbers[4] </b> <br/><br/>";
                             foreach( $numbers as $value )
                              {
                                echo "Value is <b> $value </b> <br />";
                              }
                             echo "<br/>";

                            echo "<b><i>Second method to create array(array with string)</i></b>";
                            echo "<br/><br/>";
                            $numbers[0] = "one";
                            $numbers[1] = "two";
                            $numbers[2] = "three";
                            $numbers[3] = "four";
                            $numbers[4] = "five";
                            echo "The numbers first data :- <b> $numbers[0] </b><br/>";
                            echo "The numbers second data :- <b> $numbers[1] </b><br/>";
                            echo "The numbers third data :- <b> $numbers[2] </b><br/>";
                            echo "The numbers fourth data :- <b> $numbers[3] </b><br/>";
                            echo "The numbers fifth data :- <b> $numbers[4]</b><br/><br/>";
                            foreach( $numbers as $value )
                              {
                               echo "Value is <b> $value </b> <br />";
                             }
                            ?>
                          </body>
                           </html>

                            Screenshot:-
                            

    • Associative Array :-
      • The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index.
      • It will have their index as string so that we can establish a strong association between key and values.
      • Consider an example, to store the salaries of employees in an array, we could use the employees names as the keys in the associative array, and the value would be their respective salary.
      • We should not keep associative array inside double quote while printing, otherwise it would not return any value.
      • Lets understand it further, with a suitable example
                              associative array.php :-
                              <html>
                               <body>
                               <?php

                                echo "<b><i>Creating Associative Array by using array()</i></b>";
                                echo "<br/><br/>";
                                $salaries = array(
                                    "rahul" => 2000,
                                     "ram" => 1000,
                                       "raju" => 500
                                       );
                                echo "Salary of Rahul is ". $salaries['rahul'] . "<br/>";
                                echo "Salary of Ram is ". $salaries['ram']. "<br/>";
                                echo "Salary of Raju is ". $salaries['raju']. "<br/><br/>";

                                echo "<b><i>Creating Associative Array, without using array()</i></b>";                                             echo "<br/><br/>";
                                $salaries['rahul'] = "high";
                                $salaries['ram'] = "medium";
                                $salaries['raju'] = "low";
                                echo "Salary of Rahul is ". $salaries['rahul'] . "<br />";
                                echo "Salary of Ram is ". $salaries['ram']. "<br />";
                                echo "Salary of Raju is ". $salaries['raju']. "<br />";
                                ?>
                              </body>
                              </html>

                              Screenshot:-
                              
    • Multidimensional Array :-
      • An array containing one or more arrays and values are accessed using multiple indices.
      • Let understand its concept, with an example
                              multidimensional array.php:-
                              <html>
                              <body>
                               <?php
                               $marks = array(
                               "rahul" => array
                                   (
                                  "english" => 67,
                                   "computer science" => 77
                                     ),
                                  "ram" => array
                                    (
                                    "english" => 61,
                                    "computer science" => 68
                                    ),
                                   "raju" => array
                                      (
                                    "english" => 78,
                                     "computer science" => 87
                                     )
                                   );

                                  echo "<b><i>Accessing the values of multi-dimensional array</i></b>";                                               echo "<br/><br/>";

                                  echo "Marks for Rahul in English : " ;
                                  echo $marks['rahul']['english'] . "<br />";
                                  echo "Marks for Rahul in Computer Science  : " ;
                                  echo $marks['rahul']['computer science'] . "<br /> <br />";

                                  echo "Marks for Ram in English : " ;
                                  echo $marks['ram']['english'] . "<br />";
                                  echo "Marks for Ram in Computer Science  : " ;
                                  echo $marks['ram']['computer science'] . "<br /> <br />";

                                  echo "Marks for Raju in English : " ;
                                  echo $marks['raju']['english'] . "<br />";
                                  echo "Marks for Raju in Computer Science  : " ;
                                  echo $marks['raju']['computer science'] . "<br /><br />";

                                  ?>
                                </body>
                                </html>

                                 Screenshot:-
                                 

No comments:

Post a Comment