1

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

Nick
  • 908
  • 12
  • 29

2 Answers2

3

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

}
Baba
  • 94,024
  • 28
  • 166
  • 217
  • 1
    if this doesn't break any "stack overflow terms of usage conventions", I must say: this kicks ass! as well as you @Baba! –  Oct 16 '12 at 00:08
  • Sweet! Lol at the remember to break comment. Will be testing this now! – Nick Oct 16 '12 at 00:10
  • @Nik ... this would run till your system times out – Baba Oct 16 '12 at 00:13
  • @Nick if i understand what you want .. you might not even need the array .... seriously – Baba Oct 16 '12 at 00:14
  • updated the question to reflect my use of object rather than an array. – Nick Oct 16 '12 at 00:17
  • @Baba, something like this? $infinate = new InfiniteIterator($this->rules->days) – Nick Oct 16 '12 at 00:19
  • 1
    It works for both array and and object .. see example .. i limited it to 1000 loops http://codepad.org/lw95MjlB – Baba Oct 16 '12 at 00:22
1

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!


}
rjv
  • 6,058
  • 5
  • 27
  • 49