Iterating an Array with foreach and while/list/each
<?php
$pc_parts = array();
$pc_parts["Memory"] = "256 MB";
$pc_parts["CPU"] = "Athlon Thunderbird";
$pc_parts["Video Card"] = "Matrox";
$pc_parts["Hard Drive"] = "IBM";
foreach($pc_parts as $part_type => $part_name) {
$buf = sprintf("Part name is: <strong>%s</strong> Part value is:
<strong>%s</strong>", $part_type, $part_name);
echo $buf . "<br />";
}
echo "<br /><br />";
reset($pc_parts);
while (list($part_type, $part_name) = each($pc_parts))
{
$buf = sprintf("Part name is: <strong>%s</strong> Part value is:
<strong>%s</strong>", $part_type, $part_name);
echo $buf . "<br />";
}
?>
Related examples in the same category