【问题标题】:PHP how to skip first element in while loopPHP如何在while循环中跳过第一个元素
【发布时间】:2017-11-11 16:14:19
【问题描述】:

我有在两次之间获取时间的功能,我希望这个代码像这样打印

数组([0] => 07:00:00 [1] => 08:00:00 [2] => 09:00:00 [3] => 10:00:00 [4] => 11:00:00)

(第一个元素未打印/添加)

下面的代码打印如下。

数组([0] => 06:00:00 [1] => 07:00:00 [2] => 08:00:00 [3] => 09:00:00 [4] => 10:00:00 [5] => 11:00:00)

代码

$si="06:00 AM";
$sb="11:00 AM";
$st=    date ( 'H:i:s', strtotime ($si) );
$en=date( 'H:i:s', strtotime ($sb ) );
$NoOfHours = $this->getTimesfromRange(date('H:i:s', strtotime($st)),date('H:i:s',strtotime($sb)));
print_r($NoOfHours);

函数获取时间

public function getTimesfromRange($start, $end){
        $dates = array($start);
        while(end($dates) < $end){
            if(date('H:i:s', strtotime(end($dates).' +1 hour'))==$start){
              continue;
            }else{
              $dates[] = date('H:i:s', strtotime(end($dates).' +1 hour'));
            }
        }
        return $dates;
    }

问题:如何在 while 循环中不打印第一个元素,我尝试使用 continue 但不起作用。

【问题讨论】:

  • continue 是正确的。因此,如果这不起作用,则意味着您的 if 语句实际上是错误的
  • 改为 for 循环并使用 if($i != 0){ }
  • @TomaszAdamczyk 仍在尝试其中一些,我的本地主机似乎很慢,Andreas 谢谢我试试
  • @Muklas 我添加了一个答案,但我不确定它是否正确。你有 $end,不确定它的用途以及如何将它放入代码中。我回答了从 1-> 数组末尾循环

标签: php arrays loops time while-loop


【解决方案1】:

如果我理解您的问题,您的数组中的日期是正确的,但您想打印除元素 0 以外的所有内容。

我个人会简单地复制它并删除元素 0。

$NoOfHours = $this->getTimesfromRange(date('H:i:s', strtotime($st)),date('H:i:s',strtotime($sb)));
$printable = $NoOfHours;
unset($printable[0]);
var_dump($printable);

【讨论】:

    【解决方案2】:

    假设它是您不想显示的第一个值,无论您何时可以使用计数器$i;

    public function getTimesfromRange($start, $end){
            $dates = array($start);
            $i = 0;
            while(end($dates) < $end){
                if($i != 0){
                  $dates[] = date('H:i:s', strtotime(end($dates).' +1 hour'));
                }
                $i++;
            }
            return $dates;
        }
    

    【讨论】:

    • continue; 不是消耗品吗?如果 i 为零,它将进入 {}。如果那说继续或为空没有区别?还是我错了?
    • 绝对正确的 continue 是多余的 - 实际上可以更好地重写 - 我们去
    • 按逻辑它似乎工作,当我尝试它时第一个元素仍然打印
    • 问题肯定是你的 if 语句中的表达式。为了找出为什么在第一次迭代而不是试图跳过它,var_dump if 语句中的两个值并从那里开始工作
    【解决方案3】:

    array_shift() 函数从数组中移除第一个元素,并返回移除元素的值。你可以改变你的功能如下工作。您可以找到更多详情here

    public function getTimesfromRange($start, $end){
        $dates = array($start);        
        while(end($dates) < $end){
            $dates[] = date('H:i:s', strtotime(end($dates).' +1 hour'));
        }
        array_shift($dates);
        return $dates;
    }
    

    【讨论】:

    • 对不起!我错误地返回了array_shift的结果。 array_shift() 从数组中移除第一个元素,并返回移除元素的值。所以我们应该在数组上应用array_shift(),然后需要返回数组本身。更新了代码。您可以评估相同的here
    • 我也是这么想的,我认为返回 array_shift 和返回日期是相同的,很好的解决方案,谢谢
    【解决方案4】:

    For循环排除[0]

    public function getTimesfromRange($start, $end){
            $dates = array($start);
            For($i=0; $i<count($dates)); $i++){
               If($i != 0){
                  $dates[] = date('H:i:s', strtotime(end($dates).' +1 hour'));
                }
            }
            return $dates;
        }
    

    【讨论】:

    • okeArray ( [0] => 06:00:00 ) .. 您的代码只打印第一个元素.. 感谢 Andreas 的回答
    【解决方案5】:

    只需将此代码 $dates = array($start); 更改为 $dates = [];,它不会将 $start 存储到您的数组中。你必须像这样修改你的函数,Live demo

     function getTimesfromRange($start, $end){
            $dates = [];
            while(end($dates) < $end){
                  $date = end($dates) != FALSE ? end($dates) : $start;
                  $dates[] = date('H:i:s', strtotime($date.' +1 hour'));
            }
            return $dates;
        }
    

    您也可以从结果数组中删除第一个元素,使用

    array_shift($result);unset($result[0]);array_slice($result, 1);。不推荐这些。

    【讨论】:

    • $dates = [] 只打印一个结果。,unset 正在工作
    • $dates = array($start); 更改为 $dates = []; 应该适用于您的功能。此修改仅对数组中的第一个元素进行 scape。
    • 不工作@Kris Roofe,$dates = []; 只打印单个数组,未设置工作正常,但 mkaatman 是第一次回答。谢谢克里斯
    • @Muklas,我在这里做了一个测试。您的功能有一些代码需要修改,请在此处查看。eval.in/814894。我认为不保存第一个元素会比删除第一个元素更好。
    猜你喜欢
    • 2014-01-08
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多