Monday, 23 October 2017

PHP Arrays and its types

PHP Arrays and its types
An array is a data structure that stores one or more similar type of values in a single value. For example if you want to store 100 numbers then instead of defining 100 variables it’s easy to define an array of 100 lengths.

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. An array with a numeric index. Values are stored and accessed in linear fashion.

Example
Following is the example showing how to create and access numeric arrays. Here we have used array() function to create array. This function is explained in function reference.

Numeric Array.php
<html>
   <body>
      <?php
         /* First method to create array. */
         $numbers = array( 1, 2, 3, 4, 5);
         foreach( $numbers as $value )
        {
            echo "Value is $value <br />";
         }
         /* Second method to create array. */
         $numbers[0] = "one";
         $numbers[1] = "two";
         $numbers[2] = "three";
         $numbers[3] = "four";
         $numbers[4] = "five";
         foreach( $numbers as $value )
         {
            echo "Value is $value <br />";
         }
      ?>
   </body>
</html>

This will produce the following result −
Value is 1 
Value is 2 
Value is 3 
Value is 4 
Value is 5 
Value is one 
Value is two 
Value is three 
Value is four 
Value is five

Associative Arrays
An array with strings as index. This stores element values in association with key values rather than in a strict linear index order. The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index. Associative array will have their index as string so that you can establish a strong association between key and values.

To store the salaries of employees in an array, a numerically indexed array would not be the best choice. Instead, we could use the employees names as the keys in our associative array, and the value would be their respective salary.

NOTE − don’t keep associative array inside double quote while printing otherwise it would not return any value.

Associative Arrays.php
<html>
   <body>
      <?php
         /* First method to associate create array. */
         $salaries = array("A" => 2000, "B" => 1000, "C" => 500);
         echo "Salary of A is ". $salaries['A'] . "<br />";
         echo "Salary of B is ".$salaries['B']. "<br />";
         echo "Salary of C is ".$salaries['C']. "<br />";
         /* Second method to create array. */
         $salaries['A'] = "high";
         $salaries['B'] = "medium";
         $salaries['C'] = "low";
         echo "Salary of A is ". $salaries['A'] . "<br />";
         echo "Salary of B is ".$salaries['B']. "<br />";
         echo "Salary of C is ".$salaries['C']. "<br />";
      ?>
   </body>
</html>

This will produce the following result −
Salary of A is 2000
Salary of B is 1000
Salary of C is 500
Salary of A is high
Salary of B is medium
Salary of C is low

Multidimensional Arrays
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 indexes.

Example
An array containing one or more arrays and values are accessed using multiple indices. In this example we create a two dimensional array to store marks of three students in three subjects − This example is an associative array, you can create numeric array in the same fashion.

Multidimensional Arrays.php
<html>
   <body>
      <?php
         $marks = array( 
 "A" => array ( "physics" => 35, "maths" => 30, "chemistry" => 39 ), 
 "B" => array ( "physics" => 30, "maths" => 32, "chemistry" => 29),            
           "C" => array ("physics" => 31, "maths" => 22, "chemistry" => 39)
         );
         /* Accessing multi-dimensional array values */
         echo "Marks for A in physics : " ;
         echo $marks['A']['physics'] . "<br />"; 
         echo "Marks for B in maths : ";
         echo $marks['B']['maths'] . "<br />"; 
         echo "Marks for C in chemistry : " ;
         echo $marks['C']['chemistry'] . "<br />"; 
      ?>
   </body>
</html>

This will produce the following result −
Marks for A in physics: 35
Marks for B in maths: 32
Marks for C in chemistry: 39

0 comments

Post a Comment