【问题标题】:continuously loop through PHP array (or object keys)连续循环遍历 PHP 数组(或对象键)
【发布时间】:2012-10-16 00:01:47
【问题描述】:

我有一系列对象键:

            $this->rules->days->mon = isset($this->recurring_event_data['post_ar'][$this->rules->type]['mon']) ? true : false;
            $this->rules->days->tue = isset($this->recurring_event_data['post_ar'][$this->rules->type]['tue']) ? true : false;
            $this->rules->days->wed = isset($this->recurring_event_data['post_ar'][$this->rules->type]['wed']) ? true : false;
            $this->rules->days->thu = isset($this->recurring_event_data['post_ar'][$this->rules->type]['thu']) ? true : false;
            $this->rules->days->fri = isset($this->recurring_event_data['post_ar'][$this->rules->type]['fri']) ? true : false;
            $this->rules->days->sat = isset($this->recurring_event_data['post_ar'][$this->rules->type]['sat']) ? true : false;
            $this->rules->days->sun = isset($this->recurring_event_data['post_ar'][$this->rules->type]['sun']) ? true : false;

我有这个功能:

function calc_next_weekly_interval($ii,$i)
{
         global $array;
    // we will roll through this->rules->mon,tue,wed,thu,fri,sat,sun. If its true, return the new iii value, if not keep looping through the array until we hit true again.
    $cur_day = strtolower(date('D',$ii));

    $found_today = false;

    // our initial start date
    $iii = $ii;             

    foreach($this->rules->days as $day => $value)
    {
        if($found_today == true && $value = true)
        {
            return $iii
        }
                    // need to find the current day first!
        if($day == $cur_day)
        {
            $found_today = true;
        }

        $iii + SECS_PER_DAY;            
    }
}

一切都好。注意我试图从当天找到下一个真实的日子。问题是当我使用星期日作为初始 cur_day 进行搜索时,显然 foreach 循环将在找到真正匹配之前停止。如何连续循环遍历数组(或对象键)?我应该将循环放在一个新函数中并继续使用新的开始日期调用它吗?我不想添加额外的数组键->值,因为它会影响以后的事情,我考虑过只在这个函数中添加到初始数组(这里的例子,数组被编码以供参考,但是在我的类中 - 它当然是obj keys->values

【问题讨论】:

    标签: php arrays object foreach continuous


    【解决方案1】:

    如果你想连续循环,你可以使用

    $infinate = new InfiniteIterator(new ArrayIterator($array));
    foreach ( $infinate as $value ) {
    
        // Do your thing
        // Remember to break
    
    }
    

    【讨论】:

    • 如果这不违反任何“使用约定的堆栈溢出条款”,我必须说:这太棒了!还有你@Baba!
    • 甜蜜!大声笑,记得打破评论。现在将对此进行测试!
    • @Nik ...这会一直运行到您的系统超时
    • @Nick 如果我明白你想要什么......你甚至可能不需要数组......认真
    • 它适用于数组和对象 .. 参见示例 .. 我将其限制为 1000 个循环 codepad.org/lw95MjlB
    【解决方案2】:

    怎么样

    foreach($this->rules->days as $day => $value)
    { 
        if($day == $cur_day)
        {
            $set = true;
        }
    
        $iii + SECS_PER_DAY;
    
        if($found_today == true && $value = true)
        {
            return $iii
        }
                    // need to find the current day first!
    
    
    }
    

    【讨论】:

    • 很好,但我正在努力寻找下一个真正的日子,而不是今天。
    • 对不起,刚刚意识到问题中并不清楚!问题已更新。
    猜你喜欢
    • 2014-06-18
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多