1

I have an array of numbers which is unordered list like this:-

 $a = array(
  '0' => '2',
  '1' => '4',
  '2' => '6'
  '3' => '8'
  '4' => '10'
);

Here, if i want to search number 8,then it should be return index value=3 ,but if i want to get value 3 which is not in array then it should return the nearest value

like 4= index value '1' and 2 =index value '0'.

How can i find this index value in php?

2
  • 1
    Have you tried anything so far? We would like to see your efforts. Commented Feb 19, 2015 at 6:09
  • i have some logic like if any number wont found in an array then i can find out difference between them,but how to implement in php i didn't find out. Commented Feb 19, 2015 at 6:18

2 Answers 2

2
<?php


function array_search_closest($needle, $haystack)
{
    $closest = null;
    foreach( $haystack AS $key => $value )
    {
        if( $closest === null || abs($needle - $value) < $closest[0] ) 
            $closest = [abs($needle - $value), $key];
    }

    return $closest[1];

}

$array = [6,2,10,4,8];
echo array_search_closest(3, $array); //Output: 1 

The basic functionality of that logic is to find the lowest absolute value between the needle and each of the items in the array. That value is stored into the $closest array.

The first iteration always sets the absolute value (we need something to check against). After that $closest is only overwritten if the absolute value between $needle and the value of the current iteration is lower than that of the last iteration. We eventually get lowest value, which we want.

We eventually return the key of that value.

Note that $closest is actually an array. The first element represents the current closest value of the $haystack while the last element is the key that you want.

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

3 Comments

You're welcome. A slight change could be made by adding a third parameter to decide wheter to take the high or the low distance to the given needle when both distances are the same. e.g. [2,4] and you're looking for 3. Which one should be taken (both have the same distance). That might be something you want to implement.
Better than the other answer because it doesn't do unnecessary sorting.
Eduard, you're wrong about the benefit of using native functions rather than interpreted statements. The cost of sorting is meaningless as long as you have to parse the whole array.
1

The trivial way to do it is the following:

function closest($array, $val) {
  $b=array_map(create_function('$v', 'return abs($v-'.$val.');'), $array);
  asort($b);
  return key($b);
}

echo closest(8); // returns 3

1 Comment

Awesome solution! Haven't even thought about asort(). For hipsters that could also come in closure-flavour instead of create_function() :D Joking aside. @neha you should use mark that as accepted answer, since it is much cleaner and also shorter than my solution.

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.