doujiexi1824 2011-05-07 18:53
浏览 52
已采纳

PHP for循环 - 如何逃避第一次迭代?

Given this PHP for loop

$row->frequency = 1;
$row->date_1 = 10000;
$row->interval = 86400;


    for ($i = 0; $i <= $row->frequency; $i++) {

        $cal_data[] = array(

            'start' => strtotime($row->date_1) + $row->interval,

            );
    }

I would like the first iteration of the loop to ignore the + $row->interval giving me as a result:

10000
96400

I've seen this done with modulus but couldn't manage to make it work here. Anyone have suggestions?

Thanks!

  • 写回答

2条回答 默认 最新

  • doushi7761 2011-05-07 18:55
    关注

    Use + ($i ? $row->interval : 0)

    In other words, if $i is zero - first iteration - add 0 instead of $row->interval. This is the ternary operator, (condition ? iftrue : iffalse) which is roughly equivalent to an if/else construction except it can be used amidst a statement.

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

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题