PHP array "Find nearest value to index"

Viewed 17

I would like a function to return the nearest index of an array to another value.

So IF:

$counter = 22;

With the following arrays, it should return the nearest index, probably from the $roundStartingHole variable. Or the lower index to that counter:

var_dump($roundStartingHole);
array(3) { [0]=> int(0) [1]=> int(18) [2]=> int(36) } 
array(3) { [0]=> int(17) [1]=> int(35) [2]=> int(53) }

It should return "1" (as integer).

var_dump($roundLastHole);
array(3) { [0]=> int(0) [1]=> int(18) [2]=> int(36) } 
array(3) { [0]=> int(17) [1]=> int(35) [2]=> int(53) }

It would also return "1".

0 Answers
Related