开发者

PHP: Next Available Value in an Array, starting with a non-indexed value

开发者 https://www.devze.com 2022-12-24 23:50 出处:网络
I\'ve been stumped on this PHP issue for about a day now. Basically, we have an array of hours formatted in 24-hour format, and an arbitrary value ($hour) (also a 24-hour). The problem is, we need to

I've been stumped on this PHP issue for about a day now. Basically, we have an array of hours formatted in 24-hour format, and an arbitrary value ($hour) (also a 24-hour). The problem is, we need to take $hour, and get the next available value in the array, starting with the value that immediately proceeds $hour.

The array might look something like:

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

Then the hour value might be:

$hour = 14;

So, we need some way to know that 19 is the next best time. Additionally, we might also need to get the second, third, or fourth (etc) available value.

The issue seems to be that because 14 isn't a value in the array, there is not index to reference that would let us increment to the next value.

To make things simpler, I've taken $goodHours and repeated the values several times just so I don't have to deal with going back to the start (maybe not the best way to do it, but a quick fix).

I have a feeling this is something simple I开发者_开发百科'm missing, but I would be so appreciative if anyone could shed some light.

Erik


You could use a for loop to iterate over the array, until you find the first that is greater than the one you're searching :

$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



And, to get the item you were searching for, and some after that one, you could use something like this :

$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 />";
    }
}

Which would give you the following output :

5 => 19
6 => 20


Basically, here, the idea is to :

  • advance in the array, until you find the first item that is >= to what you are looking for
    • get out of that first loop, when found
  • If a matching item was found
    • loop over the array, until either its end,
    • or you've found as many items as you were looking for.


You can also use the SPL FilterIterator. Though it's not the fastest solution there is, it has the advantage that you can "prepare" the iterator somewhere/anywhere and then pass it to a function/method that doesn't have to know how the iterator works on the inside, i.e. you could pass a completely different iterator the next time.

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

prints

19
20
21


As $goodHours is already sorted, that's something easy:

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

After that four-liner (that can be written in a smaller number of lines naturally), $next is either 0 if $hour could not be matched in $goodHours or it contains the value that immediately proceeds $hour. That is what you asked for.

This only works when $goodHours is sorted, in case it's not, you can sort it by using the asort() function.


Try this function:

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


Here's an answer similar to the rest of these, including an optional "offset" parameter, that gets your n'th item past the de-facto first one.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号