This article introduces how to use the three functions foreach, list, and each for traversing arrays in PHP:
Method 1: foreach
$sports = array(
football => good,
swimming => very well,
running => not good);
foreach ($sports as $key => $value) {
echo $key.": ".$value."
" ;
?>
Output result:
football: good
swimming: very well
running: not good
Method 2: each
$sports = array(
football => good,
swimming => very well,
running => not good);
while ($elem = each($sports)) {
echo $elem[key].": ".$elem[value]."
";
?>
Method 3: list & each
$sports = array(
football => good,
swimming => very well,
running => not good);
while (list($key, $value) = each($sports)) {
echo $key.": ".$value."< ;br />";
?>