-1

I have an array $array = [5,10,15,20,25]; and a value.

Let's say my value is 23, then I want to grab 25 because 25 is the next higher value.


I've tried in PHP

public static function findHigherValue($val,$arr){
    foreach ($arr as $key => $value) {
        if( $value <= $val){
            $higher = $value;
        }
    }

    return $higher;

}
code-8
  • 54,650
  • 106
  • 352
  • 604

8 Answers8

1

If this is javascript, the following script should solve your problem:

var $result = $array.reduce(function(a,b) { return a >= $value && a-$value < b-$value : a : b }, Infinity);
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
jehna1
  • 3,110
  • 1
  • 19
  • 29
1

I'm fairly confident one of the newer array methods would do this faster but I'd loop through

function getClosestIndex(arr, value){
   var smallestDelta = Infinity;
   var index;
   for(var i = 0; i < arr.length; i++){
       //subtract the array value from the give value
       var delta = Math.abs(arr[i] - value);
       if(delta < smallestDelta){
          smallestDelta = delta;
          index = i;
       }
   }
   return index;
}

var output = getClosestIndex($array, 13);
$array[output]; //should return the closest value

EDIT: I see the question has changed. It looks like you want closest value in the array. I've edited the code above for that.

Donnie D'Amato
  • 3,832
  • 1
  • 15
  • 40
1

If this is a javascript question, and you know the array is sorted:

var currentValue = 13;
array.find(function (value) {
    return value >= currentValue;
})

find is not supported in IE though (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)

user5325596
  • 2,310
  • 4
  • 25
  • 42
1

Try this:

var array = [5,10,15,20,25,30,35,40];
var input = 10;
var value = array.filter(function(item) {
  return item > input;
}).sort()[0];
document.getElementById('output').innerHTML = value;
<div id="output"></div>
jcubic
  • 61,973
  • 54
  • 229
  • 402
1

This solution gives the same or the next higher value from the pattern array. It works for sorted arrays only.

var pattern = [5, 10, 15, 20, 25, 30, 35, 40],
    given = [1, 13, 27, 39],
    i = 0,
    result = given.map(function (a) {
        while (pattern[i] < a) {
            i++;
        }
        return pattern[i];
    });

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Using the Array.sort() function can give unexpected results due to Unicode - See here

Would it be a good idea to run over the array using a for loop and use a simple comparitor against the previous values?

$array = [5,10,45,15,20,25,30,35,40];

var num = $array[0]; // Your first comparitor
var highest;
for (i=0; i<$array.length; i++) {
    console.log(highest);
    if($array[i] > num) {
        num = $array[i];
        highest = $array[i];
    }
}

I hope this helps.

Rolos

1

Another option using PHP, use array_filter to remove the lower value from $value and get the min value afterwards.

$value = 25;

$array = array_filter($array, function ($x) use ($value) { return $x > $value; })

echo min($array);
frz3993
  • 1,595
  • 11
  • 13
1

This returns the closest number that is higher or equal than the search value:

function findClosest(arr, val){
  return arr.reduce(function(acc, el){
    if(el >= val){
      if(acc === null) return el;
      var d1 = el - val,
          d2 = Math.abs(acc - val);
      return d1 < d2 ? el : acc;
    } else {
      return acc;
    }
  }, null);
}

console.log(findClosest([5,16,20,20,25,10,5,30], 16));

Sorted, unsorted, repeated values, etc. Shouldn't affect the outcome.

Edit: I figured 0 isn't the best value to return when there's no match. So I rewrited it a bit. This will return null if no match is found.

MinusFour
  • 13,913
  • 3
  • 30
  • 39