$prices = array('Tires'=>100,'Oil'=>10,'Spark Plugs'=>4);
foreach ($prices as $key=>$value)
echo $key.'=>'.$value.'
';
?>
最终打印出:
Tires=>100
Oil=>10
Spark Plugs=>4
$prices = array('Tires'=>100,'Oil'=>10,'Spark Plugs'=>4);
while($element = each($prices))
{
echo $element['key'];
echo '-';
echo $element['value'];
echo '
';
}
?>
最终打印出:
Tires-100
Oil-10
Spark Plugs-4
each — 返回数组中当前的键/值对并将数组指针向前移动一步
在执行 each() 之后,数组指针将停留在数组中的下一个单元或者当碰到数组结尾时停留在最后一个单元。如果要再用 each 遍历数组,必须使用 。
$foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese");
$bar = each($foo);
print_r($bar);
?>
会打印出:
Array
{
[1] => bob
[value] => bob
[0] => 0
[key] => 0
}
$prices = array('Tires'=>100,'Oil'=>10,'Spark Plugs'=>4);
while (list($product,$price) = each($prices))
echo $product.'-'.$price.'
';
reset($prices);
while (list($product,$price) = each($prices))
echo $product.'-'.$price.'
';
?>
打印出:
Tires-100
Oil-10
Spark Plugs-4
Tires-100
Oil-10
Spark Plugs-4
阅读(982) | 评论(0) | 转发(0) |