Logo

Displaying Arrays

Posted under » PHP on 26 April 2009

The foreach statement is used to loop through arrays.

<¿php
$arr=array("one", "two", "three");

foreach ($arr as $value)
{
  echo "Value: " . $value . "
"; } ?>

You can sort the arrays

$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "\n";
}

You can display only unique items

$myarray = array("lemon", "orange", "banana", "apple");
$fruits = array_unique($myarray);
foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "\n";
}