well after thinking about your post i said:
"AHA! what a fool Weedpacket must be: didn't he see me say it was more efficient"
so i sat down to prove me right, just rub it in your face. However, it turns out i was wrong, at least on my computer... though i can swear i remmber reading in some book that the while( list() = each() ) was faster
i ran some quick benchmarks using PEAR's Benchmark Iterater class... feel free to run your own test using more complex data types, or just this code on a different architecture
read more on benchmark iterater at:
http://pear.php.net/package-info.php?pacid=53
Specs:
two arrays of the alphabet, one keyed on integers, one keyed on single characters.
i ran through each array twice, one grabbing only the values, once grabbing key and value. I did this using the foreach and while techniques.
here are the results measured in seconds of execution:
WinXP : PHP 4.2.2
0.000089 : foreach() val only (integer keys)
0.000125 : while() val only (integer keys)
0.000090 : foreach() val only (string keys)
0.000132 : while() val only (string keys)
0.000097 : foreach() key and val (integer keys)
0.000142 : while() key and val (integer keys)
0.000106 : foreach() key and val (string keys)
0.000145 : while() key and val (string keys)
as you can see it turns out the foreach loop wins against the while every time in every instance, though i doubt haggling over the 5th decimal place counts much in your code..
here is the code i used to benchmark:
<?php
$int = array( "a","b","c","d","e",
"f","g","h","i","j",
"k","l","m","n","o",
"p","q","r","s","t",
"u","v","w","x","y","z");
$string = array( "a"=>"A","b"=>"B","c"=>"C",
"d"=>"D","e"=>"E","f"=>"F",
"g"=>"G","h"=>"H","i"=>"I",
"j"=>"J","k"=>"K","l"=>"L",
"m"=>"M","n"=>"N","o"=>"O",
"p"=>"P","q"=>"Q","r"=>"R",
"s"=>"S","t"=>"T","u"=>"U",
"v"=>"V","w"=>"W","x"=>"X",
"y"=>"Y","z"=>"Z" );
function loop_foreach_val( $array )
{
foreach( $array as $value ) {
//$string = $value.$value;
}
}
function loop_foreach_key_val( $array )
{
foreach( $array as $key=>$value ) {
//$string = $key.$value;
}
}
function loop_while_val( $array )
{
while( list(,$value) = each($array) ) {
//$string = $value.$value;
}
}
function loop_while_key_val( $array )
{
while( list($key,$value) = each($array) ) {
//$string = $key.$value;
}
}
echo "WinXP : PHP ". phpversion() ."<br>";
require_once "Benchmark/Iterate.php";
$benchmark = new Benchmark_Iterate;
$runs = 10000;
echo "--------------------<br>";
$benchmark->run($runs, 'loop_foreach_val', $int);
$result = $benchmark->get();
echo "{$result['mean']} : foreach() val only (integer keys)<br>";
$benchmark->run($runs, 'loop_while_val', $int);
$result = $benchmark->get();
echo "{$result['mean']} : while() val only (integer keys)<br>";
echo "--------------------<br>";
$benchmark->run($runs, 'loop_foreach_val', $string);
$result = $benchmark->get();
echo "{$result['mean']} : foreach() val only (string keys)<br>";
$benchmark->run($runs, 'loop_while_val', $string);
$result = $benchmark->get();
echo "{$result['mean']} : while() val only (string keys)<br>";
echo "--------------------<br>";
$benchmark->run($runs, 'loop_foreach_key_val', $int);
$result = $benchmark->get();
echo "{$result['mean']} : foreach() key and val (integer keys)<br>";
$benchmark->run($runs, 'loop_while_key_val', $int);
$result = $benchmark->get();
echo "{$result['mean']} : while() key and val (integer keys)<br>";
echo "--------------------<br>";
$benchmark->run($runs, 'loop_foreach_key_val', $string);
$result = $benchmark->get();
echo "{$result['mean']} : foreach() key and val (string keys)<br>";
$benchmark->run($runs, 'loop_while_key_val', $string);
$result = $benchmark->get();
echo "{$result['mean']} : while() key and val (string keys)<br>";
echo "--------------------<br>";
?>