1

For example I have array:

Array(
  [0] => 15
  [1] => 6
  [2] => 19
  [3] => 21
  [4] => 18
)

If current time is 20 then I need value 19 (which is less than and near to 20)

similarly if I pass my X number as 17 it will return 15 from array.

If 7 then it should return 6.

Please suggest how to achieve that with PHP?

Thanks!

jas
  • 177
  • 1
  • 10
  • How far have you tried? – Thamilhan Oct 27 '15 at 06:47
  • I tried by passing ( < ) condition in code from this post http://stackoverflow.com/questions/6147356/find-number-which-is-greater-than-or-equal-to-n-in-an-array but it did not return small value than my number , any suggestion what I am doing wrong ? I am not sure but I think something need to be changed with end($array) – jas Oct 27 '15 at 06:50
  • What if user pass `15` then what will be the required answer – Narendrasingh Sisodia Oct 27 '15 at 07:01
  • Then it will be less than 15 which is 6 – jas Oct 27 '15 at 07:05

3 Answers3

1

You can create your custom function using array_filter and end function like as

function find_closest($arr,$x){
    sort($arr);
    $filtered_array = array_filter($arr,function($v)use($x){ 
        return $v < $x;
    });
    return count($filtered_array) > 0 ? end($filtered_array) : "Your Message";
}

echo find_closest($arr,17);

Demo

Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
  • 1
    Thanks :) its working as it is supposed to be, can you please also suggest solution if there is no lesser value in the array. To illustrate I passed 6 but there is no smaller number than 6 in array so I want to pass some default value , how can we do that ? – jas Oct 27 '15 at 07:07
  • 1
    ok I can do that by checking result if there is something then its ok otherwise if its empty I will pass by own value. Thanks a lot for solution :) – jas Oct 27 '15 at 07:10
  • Thanks for your all help but to be fair I have to accept other guys answer as its more cleaner and faster. But this code also works great and according to requirement. – jas Oct 27 '15 at 07:58
  • 1
    Check [my_answer speed](https://3v4l.org/JgmmD/perf#tabs) and [shock_gone_wild answer speed](https://3v4l.org/ka9rF/perf#tabs) @jas – Narendrasingh Sisodia Oct 27 '15 at 09:05
  • 1
    wow @Uchiha thats great! Really great effort to check performances and making things clear. I am very thankful to all of you guys doing efforts and help for this. – jas Oct 27 '15 at 09:09
  • @shock_gone_wild that can be the case at far more bigger arrays. But not within this case – Narendrasingh Sisodia Oct 27 '15 at 09:43
  • Well, i think that the solution should satisfy any kind of input arrays. But anyway. I think both solutions are good and performance time does not matter seriously here.. – shock_gone_wild Oct 27 '15 at 09:46
  • I am not sure what to do please let me know I will accept it accordingly. I really appreciate your efforts @ [shock-gone-wild](http://stackoverflow.com/users/3927116/shock-gone-wild) and @ [Uchiha](http://stackoverflow.com/users/2899618/uchiha) – jas Oct 27 '15 at 09:46
  • 1
    Just choose the answer you are most comfortable with. It's really up to you. Maybe you should take Uchihas one, as he was first to give the right answer – shock_gone_wild Oct 27 '15 at 09:48
1

I would do it the following way. I think that's a little cleaner (and probably faster) than the accepted answer:

function closest($arr, $x) {
    $result = "default";
    sort($arr);

    foreach($arr as $value) {
        if($value < $x) {
            $result = $value;
        } else
            break;
    }
    return $result;
}
shock_gone_wild
  • 6,700
  • 4
  • 28
  • 52
  • Great thank you, its also working according to my requirements but I am not sure which one is better,off-course it looks cleaner but as you said this its more faster. So Should I go for this one ! – jas Oct 27 '15 at 07:54
-1

Push the element that you're trying to find, in your array.

array_push($your_array, "17");
$arr = asort($your_array);
$key = array_search('17', $arr); 

The value will be in $arr[$key-1]. Hope this helps you out.

Amit Horakeri
  • 747
  • 5
  • 18
  • 3
    Thanks for you help but its not working as per my requirement and answer by @Uchiha is working. – jas Oct 27 '15 at 07:09