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.

5
  • You should wrap array values in quote Commented Sep 25, 2018 at 8:00
  • 2
    Because this is string, abs doesn't work as expected. Maybe you could convert them to something more meaningful using strtotime (or something similar) before executing foreach Commented Sep 25, 2018 at 8:02
  • Yeah, this is what I thought when I edited the post. Or maybe just removing the ":" and then if i type 0535 for example it will return 0541. Does it make sense? Commented Sep 25, 2018 at 8:05
  • @lStoilov You only want to get next item of given value? Commented Sep 25, 2018 at 8:08
  • @Mohammad, yes that's correct Commented Sep 25, 2018 at 8:09

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

That's great. Works like a charm. Thanks!
I also had updated my answer to handle the values smaller than the very first element in the array.
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

3 Comments

Thats cool, but what if you type a value which is not in the array. For example 05:35. Then it will not work
If value is 05:54 then undefined offset! Wrap it with condition. :)
@Smartpal Fixed
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];
}

3 Comments

colon is not allowed in the array value, that's important for this question and it is not mentioned on your answer.
I also mentioned it in comment and do it in my demo
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.