dtudj42064 2015-06-04 14:08
浏览 20
已采纳

循环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,?),';
}
?> 
  • 写回答

2条回答 默认 最新

  • dspvin19712 2015-06-04 14:12
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数