Write a PHP Program to demonstrate the use of Array.





Write a PHP Program to demonstrate the use of Array.


<?php
/*indexed array*/
//index array using method-1
$gm=array("BCA","PGDCA","MSCIT");
echo"I like".$gm[0].",".$gm[1]."and".$gm[2].".";
echo"<br>";
//Indexed Array using Method-2
$gm[0]="<b>BCA</b>";
$gm[1]="<b>PGDCA</b>";
$gm[2]="<b>MSCIT</b>";
echo"I like".$gm[0].",".$gm[1]."and".$gm[2].".";
echo"<br>elements in array:";
$arrsize=count($gm);
for($cnt=0;$cnt<$arrsize;$cnt++)
{
echo "<br>";
echo "<br>".$gm[$cnt]."</br>";
}
echo "<br>";
/*Associative Array*/
//Associative Array using Method-1
$gm=array("d1"=>"BCA","d2"=>"PGDCA","d3"=>"MSCIT");
echo"Degree is ".$gm['d1'];
echo "<br>";




//Associative Array using Method-2
$gm['d1']="BCA";
$gm['d2']="PGDCA";
$gm['d3']="MSCIT";
echo "Degree is".$gm['d2'];
echo"<br>";
foreach($gm as $x=>$x_value)
{
echo "Key=".$x.",Value=".$x_value;
echo "<br>";
}
?>

OUTPUT:-

I likeBCA,PGDCAandMSCIT.
I likeBCA,PGDCAandMSCIT.
elements in array:

BCA


PGDCA


MSCIT

Degree is BCA
Degree isPGDCA
Key=d1,Value=BCA
Key=d2,Value=PGDCA
Key=d3,Value=MSCIT



Comments