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
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 . "
" ; }
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'] . "
"; echo $marks['Javeed'] . "
"; echo $marks['Azra'] . "
";
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 . "
" ; } ?>
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.
you can access the data in the following way: echo " Name " . " Marks " . " Grade " . "
" ; echo $student[0][0] . $student[0][1]. $student[0][2]. "
"; echo $student[1][0] . $student[1][1]. $student[1][2]. "
"; echo $student[2][0] . $student[2][1]. $student[2][2]. "
"; You can also do this with nested loop: "; }
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.
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:
Variables in PHP
PHP Concatenation
Calculation in PHP
If Statements in PHP
if ... else Statements in PHP
Nested IF Structure in PHP
PHP Comparison Operators
Switch Statement in PHP
Logical Operators In PHP
PHP Booleans
FOR Loop in PHP
While Loop In PHP
foreach Loop In PHP
Break Statement
Array In PHP
Associative Array
The Count function
Changing Case Function
Trim Function In PHP
Str_shuffle( ) function
strpos function in PHP
Explode() Function in PHP
Function
Passing Function Arguments
Server Variables
Header Function
Include Function
The require Function