1

I have an array containing lots of hours:

$hours = array(
    '06:00', '06:30', '07:00', '07:30', '08:00', '08:30', '09:00', '09:30', 
    '10:00', '10:30', '11:00', '11:30', '12:00', '12:30', '13:00', '13:30', 
    '14:00', '14:30', '15:00', '15:30', '16:00', '16:30', '17:00', '17:30', 
    '18:00', '18:30', '19:00', '19:30', '20:00', '20:30', '21:00', '21:30',
    '22:00', '22:30', '23:00', '23:30', '00:00', '00:30', '01:00', '01:30', 
    '02:00', '02:30', '03:00', '03:30', '04:00', '04:30', '05:00', '05:30', 
);

I'd like to begin looping through from a specific point e.g. '12:30'.

I have tried to use 'continue' (as below), but this is ignored. Any ideas what I am doing wrong?

foreach ($hours as $hour) {
     if ($hour == '12:30') continue;
     echo $hour . '<br>';
}
5
  • Can you debug when $hour is '12:30'? What does the if block result to when it compares '12:30' to '12:30'? Commented Jun 27, 2014 at 23:49
  • So your goal is to output all times from 12:30 forward, (or not including 12:30)? Commented Jun 27, 2014 at 23:51
  • Ok, well your for loop says in English, "go through each hour in the array. If hour is '12:30', continue to the next iteration, ignoring everything else in the loop. Otherwise, echo the hour and a line break." It is not skipping everything until 12:30 Commented Jun 27, 2014 at 23:53
  • Will the value ever occur more than once? Commented Jun 27, 2014 at 23:53
  • No the value shouldn't occur more than once as it's in 24 hr times. Commented Jun 27, 2014 at 23:55

3 Answers 3

1
$hours = array(
    '06:00', '06:30', '07:00', '07:30', '08:00', '08:30', '09:00', '09:30', 
    '10:00', '10:30', '11:00', '11:30', '12:00', '12:30', '13:00', '13:30', 
    '14:00', '14:30', '15:00', '15:30', '16:00', '16:30', '17:00', '17:30', 
    '18:00', '18:30', '19:00', '19:30', '20:00', '20:30', '21:00', '21:30',
    '22:00', '22:30', '23:00', '23:30', '00:00', '00:30', '01:00', '01:30', 
    '02:00', '02:30', '03:00', '03:30', '04:00', '04:30', '05:00', '05:30', 
);

$start = array_search('12:30',$hours);

foreach($hours as $key => $hour) {
    if($key < $start)
        continue;

    print "$hour\n";
}

Or as suggested in comments (and this is faster)

$start = array_search('12:30',$hours);

for($i = $start; $i < count($hours); $i++) {
    print $hours[$i] . "\n";
}
Sign up to request clarification or add additional context in comments.

2 Comments

Probably better to just use an incremental for loop starting from $start up to count($hours) instead of bothering with continue, looping over stuff which isn't needed.
..but yes, array_search() is the way to do this.
0

First find the key referring to '12:30', and, if found, loop over the $hours array starting at that key.

if ( $key = array_search('12:30', $hours) ) {
  for ( ; $key < count($hours); $key++ ) {
    print $hours[$key] . '<br>'; 
  } 
}

Comments

0

Change to:

if ($hour < '12:30') {
    continue;
}

Your code just skips that one time, it doesn't skip all the ones before it.

Another way to do it is to get the position of the desired hour, and use a normal for loop:

foreach ($i = array_search('12:30', $hours); $i !== false && $i < count($hours); $i++) {
    $hour = $hours[$i];
    echo $hour . "<br";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.