Yes, you can create a range and use it as the "needle", but not using in_array
. You can create a range and compute the intersection of the numbers array. To just check for any match:
$numbers = array(1105, 2140, 3170);
$needle = range(1000, 1500);
if (array_intersect($numbers, $needle)) {
echo "Match found";
} else {
echo "Match not found";
}
Or to get the possible match(es):
$numbers = array(1105, 2140, 3170);
$needle = range(1000, 1500);
$result = array_intersect($numbers, $needle);
Or you can filter out the ones not in the range:
$numbers = array(1105, 2140, 3170);
$min = 1000;
$max = 1500;
$result = array_filter($numbers, function($v) use($min, $max) {
return $v >= $min && $v <= $max;
});
You will get an empty array if there are no matches in either case. Also, you don't state what you want if there is more than one, but you'll get an array in either case so you could use current
for one or min
and/or max
instead:
$one = current(array_intersect($numbers, $needle));
$min = min(array_intersect($numbers, $needle));
$max = max(array_intersect($numbers, $needle));