Saturday, April 13, 2013

What is array


Array is collection of same data types. In PHP, an array stores multiple values in one single variable. I.e. an array is a special variable, which can store multiple values in one single variable.



using PHP, there are three types of array.


  •  Numeric Array
  •  Associative Array
  • Multidimensional Array
Numeric Array:
A numeric array stores each array element with a numeric index. i.e.  When we create numeric array, index automatically or manually assigned for each value of array at creation time.
Define array:
    
$array_name = array (array_value1, array_value2,array_value3,array_value4,--------);

$array_name[0] = array_value0;
$array_name[1] = array_value1;
$array_name[2] = array_value2;
$array_name[3] = array_value3;
..........................
..........................
$array_name[N] = array_valuen; 

Associative Array:
An associative array, each ID key is associated with a arrayvalue. When storing data about specific named values, a numerical array is not always the best way to do it. With associative arrays we can use the values as keys and assign values to them

Syntax
$ass_array = array ( "index_Id1" => arrayvalue , "index_id1" => arrayvalue, .........);

// Declare  one- dimensional array
echo "<h3>"."This is one- dimensional array"."</h3>"."</br>";
$subject = array("Mathematics","MySQL","Physics","Chemistry","English","Sanskrit","PHP","Hindi","Geography","Civics","History","Economics","Political Science");


foreach($subject as $v)
echo $v."<br>";

or

for($i=0; $i<count($subject); $i++)
{
//print_r()

echo $subject[$i]."<br><br>";
}

// Declare two-dimensional array
       $matrix  = array
                     ( array (1,2,3),
                       array (4,5,6),
                       array (7,8,9),
    array (10,11,12)
                     );
 
  echo "<h3>"."This is two dimensional array"."</h3>"."</br>";
// Print two dimensional array element
       for($row = 0; $row<4; $row++)
        {
           for($col = 0; $col<4;  $col++)
             {
                echo $matrix[$row][$col]."\t" ."\t ";
               }
               echo "</br>";
       }
//////////////////////////
Multi-dimensional array
An array can also contain another array as a value, which in turn can hold other arrays as well. In such a way we can create two- or three-dimensional arrays:
// Declare multi-dimensional array
$organization = array (array (array ("Tech","Vikash","Gupta"),
                             array  ("Sales","Sunal","Araoa"),
                             array  ("HR", "Sudhir","Aray")
                             ),
                       array (array  ("Tech","Minku","Jain"),
                             array  ("Salesteam","Guru","Gupta"),
                             array  ("HR", "Saurabh","Sharma")
                            )
                      );






echo "<h3>"."This is multidimensional Array"."</h3>"."</br>";
                    
for($layer=0; $layer<2; $layer++)
 {
  for($row=0; $row<3; $row++)
   {
    for($col=0; $col<3; $col++)
       {
        echo $organization[$layer][$row][$col]."\t";
       }
  echo "</br>" ;
 }
 echo "</br>";
 }
 ?>
 

No comments:

Post a Comment