continuously loop through PHP array (or object keys)

徘徊边缘 提交于 2019-12-12 22:26:29

问题


I have a series of object keys:

            $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;

I have this function:

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;            
    }
}

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


回答1:


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

}



回答2:


How about

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!


}


来源:https://stackoverflow.com/questions/12905700/continuously-loop-through-php-array-or-object-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!