Array In PHP

Array Declearation In PHP

Array store more then one data element with a single name. For example if you want to store the name of 10 student then you must declare 10 variable, for each student name you must declare a unique variable. But with the help of array you can store all student name with a single variable. Array store the data in continues memory location so that these element are easily accessible through index value. Where index value is a physical memory location of a single data element in array.
we declare an array in the the following way:
$student= array( );
First you type the variable name and after the equals sign, you type array function:

array( );

In PHP there are three type of array you can use:

1- Array with Numeric index which is called Index Array
2- Arrays with named keys or we say array with string as index, which is called Associative arrays
3- Arrays containing one or more arrays which is called Multidimensional arrays

Storing The values in Numeric index Array

You can assign the value to an array in different way
The first method involves typing your values between the round brackets of array().
For example: $student = array( "Majeed" , "Saleem" , "Noman" , "Qasim");
In this way all the value store in $student array in sequence index. The default initial value of index is zero.
Now if we want to print the first name of the student we write:
echo $student[0];
The above statement display the name of student which is store on zero index in our example "Majeed".
if we add some more value to $student array then we use the following statement.
$student[]="Ali" ; Now this name is store in $student, on index value 4.because index 0 to 3 already assign to previous values.
if we write the following statement:
echo $student[4];
Then it will displays the name "Ali".
You can also assign the value to an array in following way:
$student=array( );
$numbers[0] = "Majeed";
$numbers[1] = "Saleem";
$numbers[2] = "Noman";
$numbers[3] = "Qasim";
$numbers[4] = "Ali";

The best way of retrieving the value form array is loop method Fro example if we assign the 5 student name to $student then the following method is used to retrieve the store name.


 $student = array( "Majeed" , "Saleem" , "Noman" , "Qasim" ,"Ali" );

 foreach ($student as $name)

 {

    echo  $name . "<br />" ;

}

Storing The values in Associative Arrays

In Associative array data will store through String index value .For example To store the student marks in an array. We store the each student marks with there own name. The name are stored as index and marks as there values.
$marks = array("Ali" => 400, "Javeed" => 600, "Azra" => 500) ;
you can print these value


echo $marks['Ali'] . "<br />";

echo $marks['Javeed'] . "<br />";

echo $marks['Azra'] . "<br />";

You can also use the forech loop which is best way of retrieving the data form array.


foreach($marks as $x => $x_value)
{
echo "name=" . $x . "Marks=" . $x_value . "<br />" ;
}
?>

Storing The values in Multidimensional Arrays

In multi-dimensional array values are store and accessed using multiple index. You can also store the data in Numeric Two dimensional Array and also two dimensional String Index array.
For example we create a Two dimensional with Numeric Index. In which store the Name ,marks, and Grade of Student.


<?php 
$Mraks = array( array("Qasim", 500 , "A"), array("Ali", 526 , "A"), array("Javeed", 480 ,"B") ); 
?>

you can access the data in the following way:

echo "    Name    " .   "   Marks   " .  "   Grade   " .  "<br /> " ;

echo $student[0][0] . $student[0][1].  $student[0][2]. "<br />";

echo $student[1][0] . $student[1][1].  $student[1][2]. "<br />";

echo $student[2][0] . $student[2][1].  $student[2][2]. "<br />";

 

You can also do this with nested loop:

<?php

for ($x = 0; $x < 3; $x++)
    {
        for ($y = 0; $y < 3; $y++)
                {
                echo  $student[$x][$y];

                }

echo "<br/>";

}

You also store the data in two dimensional string index array or in Associative array.
For example we create a Two dimensional with string Index. In which store the Name ,marks, and Grade of Student.


<?php

$student = array(
"Ali"=> array('Marks' => 500, 'Grade' => "A"),
"Qasim"=> array('Marks' => 530, 'Grade' => "A"),
"Javeed"=> array('Marks' => 450, 'Grade' => "B")
);

?>

You can display these value form two dimension string index array in the following way:

<?php

echo $student['Ali']['Marks']. $student['Ali']['Grade'];

echo $student['Qasim']['Marks']. $student['Qasim']['Grade'];

echo $student['Javeed']['Marks']. $student['Javeed']['Grade'];

?>