duanchi6397 2013-12-12 13:44
浏览 72
已采纳

仅执行一次foreach循环[关闭]

As we know foreach loop will execute till condition does not become false. I want to execute it only once.

$i=0;
foreach($html->find('img') as $element) 
{    

       if($i!=0)  
          break;
       $logo= $element->src . '<br>';
       $i++;
}

Is there any other solution for this? like foronce in place of foreach?

  • 写回答

1条回答 默认 最新

  • dongque1646 2013-12-12 13:49
    关注

    As we know foreach loop will execute till condition does not become false.

    A foreach loop will execute once for every item of the container unless a return or break condition is defined within the foreach block.

    I want to execute it only once.

    Loops that executes once are called "don't use a loop in the first place". Here's your example fixed:

    $logo = $html->find('img')[0]->src . '<br>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料