PHP Arrays

Array declaration in php

 
      <?php
$cars = array("test1","test2");
echo $cars[0];
echo $cars[1];
?>
    
Output

Using foreach loop to traverse an array

 
      <?php
$cars = array("test1","test2");
foreach($cars as $m)
{
    echo $m;
}
?>

    
Output

Arrays can have values of different types

 
      <?php
$car = array("satish","test",1,2,2.35);
foreach($car as $m)
{
    echo $m;
}
?>
    
Output

Associative arrays - Declaration

 
      <?php
$vit = array("12bce001"=>"Satish","12bce002"=>"Mathew","12bce003"=>"Tom");
echo $vit["12bce001"];
?>
    
Output

Enter the Program title here

 
      copy and paste the code here
    
Output

Using a foreach loop to traverse an associative array

 
      <?php
$vit = array("12bce001"=>"Satish","12bce002"=>"Mathew","12bce003"=>"Tom");
foreach($vit as $reg => $name)
{
    echo $reg.$name;
}
?>
    
Output 12bce001Satish12bce002Mathew12bce003Tom

Browse the associate array to print names of only students from BCE branch

 
      <?php
$vit = array("12bce001"=>"Satish","12bci002"=>"Mathew","12bce003"=>"Tom");
foreach($vit as $reg => $name)
{
    if(strpos($reg,"bce")) //returns false if no match
    {
        echo $reg.$name;
    }
}
?>
    
Output

Create an array to hold the data given below

 
      regnumber name phonenumber 
12bce001 satish 98999899
12bce002 ram    87888889
12bce003 mathew 77877877

<?php
$vit = array(
      array("12bce001","satish","787787"),
      array("12bce002","mathew","767788"),
      array("12bce003","ram","865444"));
foreach($vit as $row)
{
    foreach($row as $value)
    {
        echo $value;
    }
    echo "<br>";
}
?>
    
Output 12bce001satish787787 12bce002mathew767788 12bce003ram865444

Enter the Program title here

 
      copy and paste the code here
    
Output

Using a simple for loop to traverse a multidimensional array

 
      copy and paste the code here
    
Output

Enter the Program title here

 
      <?php
$vit = array(
      array("12bce001","satish","787787"),
      array("12bce002","mathew","767788"),
      array("12bce003","ram","865444"));
for($row=0;$row<count($vit);$row++)
{
    for($col=0;$col<count($vit[$row]);$col++)
    {
        echo $vit[$row][$col];
    }
    echo "<br>";
}
?>
    
output 12bce001satish787787 12bce002mathew767788 12bce003ram865444 12bci002mohan988788 12bci003venkat3465444

Display the count of bce and bci students from the table

 
      <?php
$vit = array(
      array("12bce001","satish","787787"),
      array("12bce002","mathew","767788"),
      array("12bce003","ram","865444"),
      array("12bci004","mohan","98766"),
      array("12bci005","venkat","43335"));
$bcecount=0;
$bcicount=0;
foreach($vit as $row)
{
    foreach($row as $value)
    {
        if(strpos($value,"bce"))
        {
            $bcecount++;
        }
        if(strpos($value,"bci"))
        {
            $bcicount++;
        }
    }
}
echo "The bce student count is ". $bcecount;
echo "The bci student count is ". $bcicount;
?>
    
Output

Enter the Program title here

 
      copy and paste the code here
    
Output The bce student count is 3 The bci student count is 2

Create Mutidimensional Associative Array to hold the data given below

 
      12bce001:satish,12bce002:ram
      12bci0011:rahul,12bci0012:harini
      12bkt00111:mohan,12bkt00222:Arun

<?php 
$vit = array(
    array("12bce001"=>"satish","12bce002"=>"ram"),
    array("12bci0011"=>"rahul","12bci0012"=>"harini"),
    array("12bkt00111"=>"mohan","12bkt00222"=>"Arun"));

foreach($vit as $row)
{
    foreach($row as $reg=>$name)
    {
        echo $reg.$name;
    }
    echo "<br>";
}    

?> 


    
output 12bce001satish12bce002ram 12bci0011rahul12bci0012harini 12bkt00111mohan12bkt00222Arun

Sorting an array in PHP

 
      <?php
$vit = array(3,5,1,4,2);
sort($vit);
foreach($vit as $value)
{
    echo $value;
}
?>
    
Output 12345

Sorting an array in the descending order in PHP

 
      <?php
$vit = array(3,5,1,4,2);
rsort($vit);
foreach($vit as $value)
{
    echo $value;
}
?>
    
Output 54321

Sorting an associative array using keys

 
      <?php
$vit = array("12bce001"=>"satish","12bce004"=>"ram","12bce002"=>"mathew");
ksort($vit);
foreach($vit as $x=>$value)
{
    echo $x.$value;
}
?>
    
Output 12bce001satish12bce002mathew12bce004ram

Sorting an associative array using values

 
      <?php
$vit = array("12bce001"=>"satish","12bce004"=>"ram","12bce002"=>"mathew");
asort($vit);
foreach($vit as $x=>$value)
{
    echo $x.$value;
}
?>

    
Output 12bce002mathew12bce004ram12bce001satish