3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = [ ['threshold' => 3000, 'value' => 216], ['threshold' => 4000, 'value' => 254], ['threshold' => 5000, 'value' => 292], ['threshold' => 6000, 'value' => 330], ['threshold' => 7000, 'value' => 368], ['threshold' => 8000, 'value' => 406], ['threshold' => 9000, 'value' => 444], ['threshold' => 10000, 'value' => 482], ['threshold' => 13000, 'value' => 534], ['threshold' => 16000, 'value' => 586], ['threshold' => 19000, 'value' => 638], ['threshold' => 22000, 'value' => 690], ['threshold' => 25000, 'value' => 742], ['threshold' => 30000, 'value' => 812], ['threshold' => 35000, 'value' => 882], ['threshold' => 40000, 'value' => 952], ['threshold' => 45000, 'value' => 1022], ['threshold' => 50000, 'value' => 1092], ['threshold' => 65000, 'value' => 1332], ['threshold' => 80000, 'value' => 1572], ['threshold' => 95000, 'value' => 1812], ['threshold' => 110000, 'value' => 2052], ['threshold' => 125000, 'value' => 2292], ['threshold' => 140000, 'value' => 2532], ['threshold' => 155000, 'value' => 2772], ['threshold' => 170000, 'value' => 3012], ['threshold' => 185000, 'value' => 3252], ['threshold' => 200000, 'value' => 3492], ['threshold' => 230000, 'value' => 3850], ['threshold' => 260000, 'value' => 4208], ['threshold' => 290000, 'value' => 4566], ['threshold' => 320000, 'value' => 4924], ['threshold' => 350000, 'value' => 5282], ['threshold' => 380000, 'value' => 5640], ['threshold' => 410000, 'value' => 5998], ['threshold' => 440000, 'value' => 6356], ['threshold' => 470000, 'value' => 6714], ['threshold' => 500000, 'value' => 7072], ['threshold' => 550000, 'value' => 7432], ['threshold' => 600000, 'value' => 7792], ['threshold' => 650000, 'value' => 8152], ['threshold' => 700000, 'value' => 8512], ['threshold' => 750000, 'value' => 8872], ['threshold' => 800000, 'value' => 9232], ['threshold' => 850000, 'value' => 9592], ['threshold' => 900000, 'value' => 9952], ['threshold' => 950000, 'value' => 10312], ['threshold' => 1000000, 'value' => 10672] ]; function getClosest($search, $arr) { $closest = null; $i = 0; $index = 0; // Loop over the array and check if the currently closest value is higher than the current iteration foreach ($arr as $item) { if ($closest === null || abs($search - $closest['threshold']) > abs($item['threshold'] - $search)) { $closest = $item; $index = $i; } $i++; } $finalIndex = $index + 1; if($finalIndex >= count($arr)) { $finalIndex -= 1; } return floatval($arr[$finalIndex]['value']); } $result = getClosest(2500, $arr); echo $result;

preferences:
49.39 ms | 402 KiB | 5 Q