I have array with positive int values like [4, 1, 75, 52, 5, 24]
. I need to find two values with minimal difference. Also, I need the original keys of those two. So, I sorted the array with asort()
to keep the keys. Now when I iterate I have a problem - I can't use $key + 1
to point to next element and using next()
and prev()
makes it difficult to get the keys (once you use next or prev pointer is moved):
for ($i = 0; $i < count($sorted)-1; $i++) {
if (current($sorted) - next($sorted) < $min) {
//echo prev($sorted) - next($sorted) . '<br>';
}
}
What would you do?
(Feel free to alter array in any other form if that makes this easier - asort
is not necessary)
If I need to explain one more time: I have a problem with keys. Finding the closest values is not a problem.