1 回答

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超9個贊
嘗試這個
$data[]['target_download'] = 10;
$data[]['target_download'] = 20;
$data[]['target_download'] = 30;
$data[]['target_download'] = 40;
$user_download = 15;
foreach($data as $key => $val) {
if($user_download >= $val['target_download'] && $user_download < $data[++$key]['target_download']) {
//Do your stuff
echo "Range is ".$val['target_download'];
}
}
我假設(shè)您將使用數(shù)字?jǐn)?shù)組從數(shù)據(jù)庫獲取數(shù)據(jù)。
編輯 其他解決方案
$arr = array(10, 20, 30, 40);
function getClosest($search, $arr) {
$a = null;
foreach ($arr as $i) {
$a = $search >= $i ? $i : $a;
}
return $a===null ? 0:$a;
}
echo $v = getClosest(15, $arr);
- 1 回答
- 0 關(guān)注
- 129 瀏覽
添加回答
舉報