W
W
WebGuardMan2016-10-10 13:01:57
PHP
WebGuardMan, 2016-10-10 13:01:57

How to find (pick up) the closest array key?

There is an array price - product id, example

array (
1=> '215070121',
5=> '215001216',
12=> '215050121',
21=> '215010127',
50=> '215080122'
);

you need to take the product ID from the array that is the most suitable for the price, for example, at the price, you $price = 10;
should select an element with the key 12 and the value 215050121
price - only an integer
How to generate the code correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2016-10-10
@WebGuardMan

Bisection, and then selecting the closest left and right neighbor if no exact match is found.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question