2

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.

Mohammad
  • 21,175
  • 15
  • 55
  • 84
lStoilov
  • 1,256
  • 3
  • 14
  • 30

3 Answers3

2

Using array_search() you can find index of array item based of it value. So use it to getting index of searched item.

function getClosest($search, $arr) {
    return $arr[array_search($search, $arr)+1];
}

Update:

If search value not exist in array or search value is last item of array function return empty.

function getClosest($search, $arr) {
    $result = array_search($search, $arr);  
    return $result && $result<sizeof($arr)-1 ? $arr[$result+1] : "";
}

Check result in demo

Mohammad
  • 21,175
  • 15
  • 55
  • 84
2

Using the internal pointer array iterator -which should be better from the performance point of view than array_search- you can get the next value like this:

$arr = array('05:11','05:21','05:24','05:31','05:34','05:41','05:44','05:50','05:54');
function getClosest($search, $arr) {

    $item = null;
    while ($key = key($arr) !== null) {
        $current = current($arr);
        $item = next($arr);
        if (
            strtotime($current) < strtotime($search) &&
            strtotime($item) >= strtotime($search)
        ) {
            break;
        } else if (
            strtotime($current) > strtotime($search)
        ) {
            $item = $current;
            break;
        }
    }

    return $item;
}

print_r([
    getClosest('05:50', $arr),
    getClosest('05:34', $arr),
    getClosest('05:52', $arr),
    getClosest('05:15', $arr),
    getClosest('05:10', $arr),
]);

This will output :-

Array (
    [0] => 05:50
    [1] => 05:34
    [2] => 05:54
    [3] => 05:21
    [4] => 05:11
)

Live example https://3v4l.org/tqHOC

hassan
  • 7,812
  • 2
  • 25
  • 36
0

First convert your array value in string because of you use the ':' in the value like

array('05:11','05:21','05:24','05:31','05:34','05:41','05:44','05:50','05:54');

Then use below code to find the next value from the array

function getClosest($search, $arr) {
  return $arr[array_search($search,$arr) + 1];
}
Dolar
  • 423
  • 2
  • 10
  • colon is not allowed in the array value, that's important for this question and it is not mentioned on your answer. – Dolar Sep 25 '18 at 08:20
  • I also mentioned it in comment and do it in my demo – Mohammad Sep 25 '18 at 08:22
  • Sorry i haven't read your comment before the post, if you want then i will provide the screenshot of the code that i made for this question :) And it's common and easiest technique to find the value in the array. – Dolar Sep 25 '18 at 08:23