PHP Array and Foreach loop Tutorial with examples

PHP Array and Foreach loop Tutorial with examples

Array:- Array is a sequence of data in memory. which placed in physically adjacent  location.
In php there are two type of array.
a) Index based array ( one dimensional  /multi dimensional )
b) Associative array ( one dimensional  /multi dimensional )
Here  is an example of array, which is  similar to other language.
pro1.php
<?php
$ar[0]=5;
$ar[1]=10;
$ar[2]=20;
$ar[3]=30;
$tot=$ar[0]+$ar[1]+$ar[2]+$ar[3];
echo “Result=”.$tot;
?>

One dimensional index based array.

Php provides a language construct  array() for  creation of array. In the following example 5 values are placed in array(5,10,20,30). Here element will automatic  store on index 0, 1, 2, 3.
pro2.php
<?php
$ar=array(5,10,20,30);
$tot=$ar[0]+$ar[1]+$ar[2]+$ar[3];
echo “Result=”.$tot;
?>

Accessing element of array using loop…

pro3.php
<?php
$ar=array(5,10,20,30,45,25);
$i=0;
$tot=0;
while($i<6)
{
$tot=$tot+$ar[$i];
$i++;
}
echo “Result=”.$tot;
?>
In the previous example size of array is manually counted. Now use count() function for calculating size of array. count () function takes an array as a parameter and and return no of element of array.
pro4.php
<?php
$ar=array(5,10,20,30,45,25);
$i=0;
$tot=0;
$size=count($ar);
while($i<$size)
{
$tot=$tot+$ar[$i];
$i++;
}
echo “Result=”.$tot;
?>

 

Multi dimensional index based array. An array which store both array and value.

pro5.php
<?php
$m=array(array(5,8,12,45),56,array(8,52,54,32,56,58));
$i=0;
while($i<count($m))
{
$ea=$m[$i];
if(is_array($ea))
{
$ei=0;
while($ei<count($ea))
{
echo $ea[$ei].”<br>”;
$ei++;
}
}
else
echo $ea.”<br>”;
$i++;
}
?>

If programmer wants to create an array  and associate them with own keys, that type of array is associative  array.
Syntax of associative array

$array_name=array(“key”=>”value”).
Example of associative array.
Pro6.php
<?php
$arr=array(“d1″=>50,”f2″=>20,”b5″=>30);
foreach($arr as $v)
{
echo $v.”<br>”;
}
?>
This  program  output the list of state and capital in tabular form. Save and run this programm.
Pro7.php
<?php
$state=array(“Bihar”=>”Patna”,”UP”=>”Lucknow”,”Delhi”=>”Delhi”,”Punjab”=>”Chandigarh”);
echo”<table border=’1′>”;
foreach($state as $k=>$v)
{
echo “<tr><td>$k</td><td>$v</td></tr>”;
}
echo “</table>”;
?>
In the last example ,element of associative array is retrieved using foreach loop.  The element of associative array can also accessed with the help of each() and list() function which is illustrated in the following example.
pro8.php
<?php
$state=array(“Bihar”=>”Patna”,”UP”=>”Lucknow”,”Delhi”=>”Delhi”,”Punjab”=>”Chandigarh”);
while(list($k,$v)=each($state))
{
echo $k.” “.$v.”<br>”;
}
?>
Program of list and sublist using multi dimension array. Copy and run this program.
pro9.php
<?php
$st=array(“Bihar”=>array(“Sitamarhi”,”Darbhanga”,”Madhubani”),
“Up”=>array(“Balia”,”Baranasi”),”Punjab”=>array(“Sonipath”,”Panipath”,”Ambala”));
$state=$_POST[‘state’];
?>

<form action=”” method=”post”>
<table width=”426″ border=”1″ align=”center”>
<tr>
<td width=”228″ height=”35″><label>
Select State<select name=”state”>
<?php
if(isset($state))
echo”<option selected=’$state’>$state</option>”;
foreach($st as $k=>$v)
{
echo “<option value=’$k’>$k</option>”;
}
?>
</select>
</label></td>
<td width=”182″><label>
City
<select name=”city”>
<?php
foreach($st as $k=>$v)
{
if($state==$k)
{
foreach($v as $p)
echo “<option value=’$p’>$p</option>”;
}
}
?>
</select>
</label></td>
</tr>
<tr>
<td height=”34″><label>
<input type=”submit” name=”b1″ value=”ok”>
</label></td>
<td>&nbsp;</td>
</tr>
</table>
</form>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Do you have any Query / Suggestion