以下方法有什麼區別嗎?Foreach vs list-each in PHP
$fruit = array(
'a' => 'apple',
'b' => 'banana',
'c' => 'cranberry'
);
// List-each method
reset($fruit);
while (list($key, $val) = each($fruit)) {
echo "$key => $val\n";
}
// foreach method
foreach ($fruit as $key => $value) {
echo "$key => $val\n";
}
第二種方法('foreach')更快。 –
哪一個看起來更清潔? –
http://www.phpbench.com/或http://m.metamorphosite.com/php-benchmarks-loops-arrays#Foreach –