PHP Arrays is used to save multiple values in a single variable.
What is an PHP Array?
PHP array is variable which can store more than one value at a time.
Example, If you have many fruits name , then you can save all fruits in single fruit variable.
Array has index key and elements.
How to create PHP Array?
We can create array using array() function;
PHP Syntax:
array("value");
array("key"=>"value");
Type of PHP Array?
There are 3 types of array in PHP.
- Indexed Array - It has numeric index key.
- Associative Array - It has string based index key
- Multidimensional Array - It contains one or more arrays
Indexed Array
There are two ways to define indexed array.
1st way
If you do not define index key in array, then Index key numbers will be started at zero (0) by default.
$day=array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
2nd way
$day[0]="Sunday";
$day[1]="Monday";
$day[2]="Tuesday";
$day[3]="Wednesday";
$day[4]="Thursday";
$day[5]="Friday";
$day[6]="Saturday";
PHP Example :
<?php
$day=array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
echo "<br/>Fetch array value using foreach loop<br/>";
foreach($day as $key=>$val)
{
echo "<br/>Day $key : ".$val;
}
echo "<br/><br/>Also Get array value using index number as per as below<br/>";
echo "<br/>Day 1 : ".$day[0];
echo "<br/>Day 2 : ".$day[1];
echo "<br/>Day 3 : ".$day[2];
echo "<br/>Day 4 : ".$day[3];
echo "<br/>Day 5 : ".$day[4];
echo "<br/>Day 6 : ".$day[5];
echo "<br/>Day 7 : ".$day[6];
?>
Output:
Fetch array value using foreach loop
Day 0 : Sunday
Day 1 : Monday
Day 2 : Tuesday
Day 3 : Wednesday
Day 4 : Thursday
Day 5 : Friday
Day 6 : Saturday
Also Get array value using index number as per as below
Day 1 : Sunday
Day 2 : Monday
Day 3 : Tuesday
Day 4 : Wednesday
Day 5 : Thursday
Day 6 : Friday
Day 7 : Saturday
Associative Array
Index key will be string in associative array.
$marks=array("arya"=>"89","Dirgh"=>"70","Deep"="35","Reyansh"=>"95");
PHP Example :
<?php
$marks=array("arya"=>"89","Dirgh"=>"70","Deep"=>"35","Reyansh"=>"95");
$marks['Pal']="67"; // Also you can assign marks in $marks array variable
echo "<br/>Get students marks using foreach loop<br/>";
foreach($marks as $key=>$val)
{
echo "<br/>Student - $key : ".$val;
}
echo "<br/><br/>Also Get students marks using name as per as below<br/>";
echo "<br/>Student - Dirgh: ".$marks["Dirgh"];
echo "<br/>Student - Reyansh : ".$marks["Reyansh"];
echo "<br/>Student - arya : ".$marks["arya"];
echo "<br/>Student - Pal : ".$marks["Pal"];
?>
Output:
Get students marks using foreach loop
Student - arya : 89
Student - Dirgh : 70
Student - Deep : 35
Student - Reyansh : 95
Student - Pal : 67
Also Get students marks using name as per as below
Student - Dirgh: 70
Student - Reyansh : 95
Student - arya : 89
Student - Pal : 67
Multidimensional Array
PHP multidimensional array is also called as array of arrays.
PHP multidimensional array can be represented in the form of matrix which contains row and column.
There are two dimensional array, three dimensional array, four dimensional array and so on...
PHP Example :
<?php
$mobile = array("Samsung"=>array("M20"=>10000,"M31"=>"19999"),"Redme"=>array("Note 7"=>7000,"Note 8"=>11300),"Nokia"=>array("J10"=>20000,"J5"=>3400));
foreach($mobile as $name=>$model_arr)
{
foreach($model_arr as $model=>$price)
{
echo "<br/> <b>Mobile :</b> $name <b>Model :</b> $model <b>Price :</b> $price <br/>";
}
}
?>
Output:
Mobile : Samsung Model : M20 Price : 10000
Mobile : Samsung Model : M31 Price : 19999
Mobile : Redme Model : Note 7 Price : 7000
Mobile : Redme Model : Note 8 Price : 11300
Mobile : Nokia Model : J10 Price : 20000
Mobile : Nokia Model : J5 Price : 3400
Comments