PHP: the next available value in the array, starting with an unindexed value

I have been baffled by this problem with PHP for about a day now. Basically, we have an array of clocks formatted in a 24-hour format, and an arbitrary value ( $hour) (also a 24-hour clock). The problem is that we need to take $hourand get the next available value in the array, starting with the value that continues immediately $hour.

An array might look something like this:

$goodHours = array('8,9,10,11,12,19,20,21).

Then the hour value can be:

$hour = 14;

So we need a way to find out that 19 is the best, best time. In addition, we may also need to obtain a second, third or fourth (etc.) available value.

The problem is that since 14 is not a value in the array, there is no index for the link that allows us to increment to the next value.

To make things easier, I took $goodHoursand repeated the values ​​several times so that I did not have to deal with a return to the beginning (maybe this is not the best way to do this, but a quick solution).

I have a feeling that it’s just that I’m missing, but I would be so grateful if anyone could shed some light.

Eric

+5
source share
5 answers

You can use the for loop to iterate through the array until you find the first one that is larger than the one you are looking for:

$goodHours = array(8,9,10,11,12,19,20,21);
$hour = 14;

$length = count($goodHours);
for ($i = 0 ; $i < $length ; $i++) {
    if ($goodHours[$i] >= $hour) {
        echo "$i => {$goodHours[$i]}";
        break;
    }   
}

Would give you:

5 => 19



, , , , - :

$goodHours = array(8,9,10,11,12,19,20,21);
$hour = 14;
$numToFind = 2;

$firstIndex = -1;
$length = count($goodHours);
for ($i = 0 ; $i < $length ; $i++) {
    if ($goodHours[$i] >= $hour) {
        $firstIndex = $i;
        break;
    }   
}

if ($firstIndex >= 0) {
    $nbDisplayed = 0;
    for ($i=$firstIndex ; $i<$length && $nbDisplayed<$numToFind ; $i++, $nbDisplayed++) {
        echo "$i => {$goodHours[$i]}<br />";
    }
}

:

5 => 19
6 => 20


, :

  • , , >=, ,
    • ,
    • , ,
    • , .
+4

SPL FilterIterator. , , "" -/ , /, , , .. .

class GreaterThanFilterIterator extends FilterIterator {
  protected $threshold;
  public function __construct($threshold, Iterator $it) {
    $this->threshold = $threshold;
    parent::__construct($it);
  }

  public function accept() {
    return $this->threshold < parent::current();
  }
}

function doSomething($it) {
  // no knowledge of the FilterIterator here
  foreach($it as $v) {
    echo $v, "\n";
  }
}

$goodHours = array(8,9,10,11,12,19,20,21);
$it = new GreaterThanFilterIterator(14, new ArrayIterator($goodHours));
doSomething($it);

19
20
21
+3

$goodHours , - :

$next = 0;
foreach($goodHours as $test)
   if($test > $hour && $next = $test)
       break;

( ), $next 0, $hour $goodHours, , $hour. , .

, $goodHours , asort().

+1

Try this feature:

function nextValueGreaterThan($haystack, $needle, $n=1) {
    sort($haystack);
    foreach ($haystack as $val) {
        if ($val >= $needle) {
            $n--;
            if ($n <= 0) {
                return $val;
            }
        }
    }
}

$goodHours = array(8,9,10,11,12,19,20,21);
echo nextValueGreaterThan($goodHours, 14);     // 19
echo nextValueGreaterThan($goodHours, 14, 3);  // 21
0
source

Here the answer is similar to the rest of them, including the optional "offset" parameter, which gets your nth element past the de facto first.

class GoodHours {
  private $hours = array(8,9,10,11,12,19,20,21);

  public function getGoodHour($hour, $offset = 0) {
    $length = count($this->hours);
    for ($i = 0 ; $i < $length && $this->hours[$i] < $hour ; $i++)
      ; // do nothing
    return $this->hours[($i + $offset) % $length];
  }
}

// some test values

$good = new GoodHours();
$x = $good->getGoodHour(5);    // 8
$x = $good->getGoodHour(5,1);  // 9
$x = $good->getGoodHour(5,2);  // 10
$x = $good->getGoodHour(10);   // 10
$x = $good->getGoodHour(10,1); // 11
$x = $good->getGoodHour(10,2); // 12
$x = $good->getGoodHour(21);   // 21
$x = $good->getGoodHour(21,1); // 8
$x = $good->getGoodHour(21,2); // 9
$x = $good->getGoodHour(21);   // 8
$x = $good->getGoodHour(22,1); // 9
$x = $good->getGoodHour(22,2); // 10
0
source

All Articles