循环PHP数组的最有效方法[关闭]

I need to iterate over an array and do something where the actual value of the array is not needed. What is the most efficient way to do so? If I do need to use the array element value, does the answer change? I've given two possible solutions below.

<?php
$sql='INSERT INTO bla(x,y,z) ';
$myarray=array(4,3,6,9,6,3,6);

for ($i = 0; $i <= count($myarray); $i++) {
    //Do something where $myarray[$x] is not needed such as make a sting
    $sql.='(1,2,?),';
}

foreach($myarray as $arrayElement) {
    //Do something where $arrayElement is not needed such as make a sting
    $sql.='(1,2,?),';
}
?> 

As far as I know, for is slightly faster than foreach, thus making it more efficient. Of course, the difference is so small that I doubt anyone can notice it in most occasions.

But foreach seems to be a more elegant way, and also in case of associative array it is your only choice.

If you don't need to use the value, then for is a bit faster. If you need to alter the values of the array then the foreach loop with reference is the fastest. See https://stackoverflow.com/a/3433065/2078780