Thursday, 8 March 2018

Array Example In PHP

 Indexed Array
      1)    Indexed Array  Example(1st method)
<?php
echo "Indexed Array<br>";
$season=array("Summer","Monsoon","Winter","Spring");//Array Declaration
echo "Method 1 : <br>";
echo "Seasons are : $season[0], $season[1],$season[2],$season[3]<br>";
?>
Output:
Indexed Array
Method 1 : 
Seasons are : Summer, Monsoon,Winter,Spring
2) Indexed Array  Example( 2nd  method)
<?php
echo "Indexed Array<br>";
$season[0]="Summer";
$season[1]="Monsoon";
$season[2]="Winter";
$season[3]="Spring";
echo "<br>Method 2 : <br> Seasons are : $season[0], $season[1],$season[2],$season[3]<br>";
?>
Output:
Indexed Array
Method 2 :
Seasons are : Summer, Monsoon,Winter,Spring

3) Indexed Array  Example( 3rd  method)
<?php
echo "Indexed Array<br>";
$season=array("Summer","Monsoon","Winter","Spring");//Array Declaration

echo"<br>Method 3: Using Foreach loop<br>";
foreach($season as $s)
{
          echo"Season is $s<br>";
}
?>
Output:
Indexed Array
Method 3: Using Foreach loop
Season is Summer
Season is Monsoon
Season is Winter
Season is Spring
4) Indexed Array Example (4th method)
<?php
echo "Indexed Array<br>";
$season=array("Summer","Monsoon","Winter","Spring");//Array Declaration
echo"<br> Method 4 : Using for loop<br>";
$arrlen = count($season);
{
          for($i=0;$i<$arrlen;$i++)
          {
                   echo $season[$i] ."</br>";
          }       
}
?>
Output:
Indexed Array
Method 4 : Using for loop
Summer
Monsoon
Winter
Spring


Associative Array
1) Associative Array Example(1st method)
<?php
echo "<br>Associative Array<br>";
$sal=array("SRK"=>"100", "Salya"=>"150", "Rajni"=>"500", "AK"=>"50");
echo " Method 1 :<br> ";
echo "SRK Salary"." "." ".$sal["SRK"]."</br>";
echo "Salya Salary"." "." ".$sal["Salya"]."</br>";
echo "Rajni Salary"." " . " " .$sal["Rajni"]."</br>";
echo "AK Salary"." "." " .$sal["AK"]."</br>";
?>
Output:
Associative Array
Method 1 :
SRK Salary 100
Salya Salary 150
Rajni Salary 500
AK Salary 50
2) Associative Array Example(2nd  method)
<?php
echo "<br>Associative Array<br>";
$sal["SRK"]=100;
$sal["Salya"]=150;
$sal["Rajni"]=500;
$sal["AK"]=50;
echo"<br>Method 2<br>";
echo "SRK Salary"." "." ".$sal["SRK"]."</br>"; 
echo "Salya Salary". " " .$sal["Salya"]."<br>";
echo "Rajni Salary". " " .$sal["Rajni"]."<br>";
echo "AK Salary". " " .$sal["AK"]."<br>";
?>
Output:
Associative Array
Method 2
SRK Salary 100
Salya Salary 150
Rajni Salary 500
AK Salary 50
3) Associative Array Example(3rd  method)
<?php
echo "<br>Associative Array<br>";
$sal=array("SRK"=>"100", "Salya"=>"150", "Rajni"=>"500", "AK"=>"50");
echo"Method 3 - foreach loop<br>";
foreach($sal as $k => $v)
{
          echo"Key:".$k." ". "Value".$v."</br>";
}
?>
Output:
Associative Array
Method 3 - foreach loop
Key:SRK Value100
Key:Salya Value150
Key:Rajni Value500
Key:AK Value50

Multidimensional Array
1) Multidimensional Array Example (1st method)
<?php
echo"<br> Multidimensional Array <br>";
$emp = array
(
array(1, "Shashank", 500),
array(2, "Dhananjay", 550),
array(3, "Yasho", 600)
);

$arrl=count($emp);

for($r=0;$r<$arrl;$r++)
          {
                   for($c=0;$c<$arrl;$c++)
                   {
                             echo $emp[$r][$c]." ";
                   }
                   echo"<br>";
          }

          ?>
Output:
Multidimensional Array 
1 Shashank 500 
2 Dhananjay 550 
3 Yasho 600 

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...