将您要搜索的数字作为第一个参数,将数字数组作为第二个参数:
function getClosest($search, $arr) { $closest = null; foreach ($arr as $item) { if ($closest === null || abs($search - $closest) > abs($item - $search)) { $closest = $item; } } return $closest;}


