I am trying to compare a values in array and select the next value in the array based on a selected value.
For example
array(05:11,05:21,05:24,05:31,05:34,05:41,05:44,05:50,05:54);
and if the the search value is for example 05:34
, the one that is returned to be 05:41
. If the value is 05:50
, 05:54
is returned
I did find something that might help me on this post, but because of the :
in my values it will not work.
Any ideas how can I get this to work?
function getClosest($search, $arr) {
$closest = null;
foreach ($arr as $item) {
if ($closest === null || abs($search - $closest) > abs($item - $search)) {
$closest = $item;
}
}
return $closest;
}
UPDATE Maybe I should somehow convert the values in the array in something more convenient to search within - Just a thinking.