Php 确定值是否在数组的范围内
我有一个如下所示的数组:Php 确定值是否在数组的范围内,php,arrays,filter,range,Php,Arrays,Filter,Range,我有一个如下所示的数组: [age-pref] => Array ( [0] => 31-35 ) [age-pref] => Array ( [0] => 31-35,36-40 ) 我通过以下方法确定学生的年龄是否在此范围内: $search_age = $filters['age-pref']; list($age_from, $age_to ) =
[age-pref] => Array
(
[0] => 31-35
)
[age-pref] => Array
(
[0] => 31-35,36-40
)
我通过以下方法确定学生的年龄是否在此范围内:
$search_age = $filters['age-pref'];
list($age_from, $age_to ) = explode('-', $search_age[0]);
if( !empty($age_from) && !empty($age_to) ){
$result_age = ( $student_field['student_age'][0] >= $age_from && $student_field['student_age'][0] <= $age_to ) ? true : false;
}else{
$result_age = true;
}//endif
我很难比较它们。有人能帮我解释一下逻辑吗
谢谢 函数isAgeInRange($age,$ranges){
function isAgeInRange($age, $ranges) {
if (empty($ranges)) return true;
foreach (explode(',', $ranges) as $range) {
$range = trim($range);
list($from, $to) = explode('-', $range);
if ($age >= $from && $age <= $to) return true;
}
return false;
}
$result_age = isAgeInRange($student_field['student_age'][0], $filters['age-pref'][0]);
if(空($ranges))返回true;
foreach(将(“,”,$ranges)分解为$range){
$range=修剪($range);
列表($from,$to)=分解('-',$range);
如果($age>=$from&&$age$student\u age=43;
$age\u pref=数组(
"31-35,36-40"
);
范围内的函数($age,$range)
{
$chunks=explode(“,”,$range[0]);
foreach($chunk作为$chunk)
{
$val=分解(“-”,$chunk);
如果($age>=(int)$val[0]&&&$age
$student_age = 43;
$age_pref = array(
"31-35,36-40"
);
function inrange($age, $range)
{
$chunks = explode(",", $range[0]);
foreach($chunks as $chunk)
{
$val = explode("-", $chunk);
if ($age >= (int)$val[0] && $age <= (int)$val[1])
{
return true;
}
}
}
echo inrange($student_age, $age_pref);