An array is a data structure that stores one or more similar type of values in a single variable.
There are 3 types of array in PHP :
- Indexed Array
- Associative Array
- Multidimensional Array
Indexed Arrays : Index array is represented by number which starts from 0. There are two ways to create indexed arrays.
First way :
Second way :
$student = array("Alokesh", "Ashim", "Mridul");
Second way :
or the index can be assigned manually:
$student[0] = "Alokesh";
$student[1] = "Ashim";
$student[2] = "Mridul";
$student[0] = "Alokesh";
$student[1] = "Ashim";
$student[2] = "Mridul";
Example :
Output :
Or
Output :
Associative Array : Associative arrays are arrays that use named keys that you assign to them. There are two ways to create an associative array.
First way :
Second way :
Example :
Output :
Multidimensional Array : A multidimensional array is an array containing one or more arrays. Multidimensional array can be represented in the form of matrix which is represented by row * column.
Let's see a simple example of multidimensional array to display following tabular data.
Using array :
Output :
<?php
$student = array("Alokesh", "Ashim", "Mridul");
echo "Students name : ".$student[0].", ".$student[1].", ".$student[2]."<br>";
/* count() function is used to return the length of an array */
echo " Length of the array is ".count($student);
?>
$student = array("Alokesh", "Ashim", "Mridul");
echo "Students name : ".$student[0].", ".$student[1].", ".$student[2]."<br>";
/* count() function is used to return the length of an array */
echo " Length of the array is ".count($student);
?>
Output :
Students name : Alokesh, Ashim, Mridul
Length of the array is 3
Length of the array is 3
Or
<?php
$student[0]="Alokesh";
$student[1]="Ashim";
$student[2]="Mridul";
echo "Students name : ".$student[0].", ".$student[1].", ".$student[2]."<br>";
?>
$student[0]="Alokesh";
$student[1]="Ashim";
$student[2]="Mridul";
echo "Students name : ".$student[0].", ".$student[1].", ".$student[2]."<br>";
?>
Output :
Students name : Alokesh, Ashim, Mridul
Associative Array : Associative arrays are arrays that use named keys that you assign to them. There are two ways to create an associative array.
First way :
$age = array("Alokesh"=>"23", "Ashim"=>"22", "Mridul"=>"22");
Second way :
$age["Alokesh"] = "23";
$age["Ashim"] = "22";
$age["Mridul"] = "22";
$age["Ashim"] = "22";
$age["Mridul"] = "22";
Example :
<?php
$age = array("Alokesh"=>"23", "Ashim"=>"22", "Mridul"=>"22");
echo "Alokesh is ".$age["Alokesh"]." year old <br>";
echo "Ashim is ".$age["Ashim"]." year old <br>";
echo "Mridul is ".$age["Mridul"]." year old";
?>
$age = array("Alokesh"=>"23", "Ashim"=>"22", "Mridul"=>"22");
echo "Alokesh is ".$age["Alokesh"]." year old <br>";
echo "Ashim is ".$age["Ashim"]." year old <br>";
echo "Mridul is ".$age["Mridul"]." year old";
?>
Output :
Alokesh is 23 year old
Ashim is 22 year old
Mridul is 22 year old
Ashim is 22 year old
Mridul is 22 year old
Multidimensional Array : A multidimensional array is an array containing one or more arrays. Multidimensional array can be represented in the form of matrix which is represented by row * column.
Let's see a simple example of multidimensional array to display following tabular data.
ID | Name | Salary |
---|---|---|
1 | Rahul | 45000 |
2 | Pankaj | 38000 |
3 | Parag | 40000 |
Using array :
<?php
$client = array
(
array(1,"Rahul",45000),
array(2,"Pankaj",38000),
array(3,"Parag",40000),
);
for ($row = 0; $row < 3; $row++) {
for ($col = 0; $col < 3; $col++) {
echo $client[$row][$col]." ";
}
echo "<br/>";
}
?>
$client = array
(
array(1,"Rahul",45000),
array(2,"Pankaj",38000),
array(3,"Parag",40000),
);
for ($row = 0; $row < 3; $row++) {
for ($col = 0; $col < 3; $col++) {
echo $client[$row][$col]." ";
}
echo "<br/>";
}
?>
Output :
1 Rahul 45000
2 Pankaj 38000
3 Parag 40000
2 Pankaj 38000
3 Parag 40000
0 Comments