PHP Programming

Hands On No. 14 : Working with Arrays in PHP

Working with Arrays in PHP

Simple Arrays

                                

<?php

$cars = array("Volvo", "AUDI", "TATA");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";

?>

Indexed Arrays
                                

<?php

$cars = array("Volvo", "AUDI", "TATA");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";

?>


<?php

$cars = array("Volvo", "AUDI", "TATA");
$arrlength = count($cars);
for($x = 0; $x < $arrlength; $x++)
{
echo $cars[$x];
echo "<br>";
}

?>

Associative Arrays
                                

<?php

$age = array("Roop"=>"45", "Krish"=>"49", "Dino"=>"23");
echo "RAM is " . $age['RAM'] . " years old.";

?>


<?php

$age = array("Roop"=>"45", "Krish"=>"49", "Dino"=>"23");

foreach($age as $x => $x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}

?>