Array In PHP

Associative Array in PHP

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 valu


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 />" ;
}
?>

Two Dimensional String Index Associative Array

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'];

?>