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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据