通过PHP数组连续循环(或对象键) [英] continuously loop through PHP array (or object keys)

查看:145
本文介绍了通过PHP数组连续循环(或对象键)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列对象键的:

            $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循环将停止。我怎么能不断通过一个数组(或对象键)循环?我应该把一个新的功能循环,不断有新的开始日期调用它?我不想增加额外的数组键 - >值,因为它会影响以后的事情,我曾经想过在这里只在这个函数中添加初始阵列(例如,数组参考codeD,但在我类 - 这是当然的OBJ键 - >值

all good. Note I am trying to find the next true day from the current day. Issue is when I do a search using a Sunday as the initial cur_day, obviously the foreach loop will stop before it finds a true match. How can I continuously loop through an array (Or object keys)? Should I put the loop in a new function and keep calling it with a new start date? I don't want to add extra array keys->values as it will affect things later, I have thought about adding to the initial array only in this function (example here, the array is coded for reference, but in my class - it is of course obj keys->values

推荐答案

如果要循环不断,您可以使用

If you want to loop continually you can use

$infinate = new InfiniteIterator(new ArrayIterator($array));
foreach ( $infinate as $value ) {

    // Do your thing
    // Remember to break

}

这篇关于通过PHP数组连续循环(或对象键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆