我有一个数组,我需要比较来自mysql的数据。通常我会做一个直接比较,所以我可以做if ($array[$i]===$mysql[$i])
,但我确实有一个实例,我需要将它与一系列数字匹配(例如18-19,20-24等)。我查看了preg_match& preg_grep,但它们似乎不是我想要的......
我只需要比较中的真/假结果。
我想要匹配的数组部分如下所示:
"age"=>array(
'18-19'=>array('total'=>0,'completed'=>0),
'20-24'=>array('total'=>0,'completed'=>0),
'25-29'=>array('total'=>0,'completed'=>0),
'30-34'=>array('total'=>0,'completed'=>0),
'35-39'=>array('total'=>0,'completed'=>0),
'40-44'=>array('total'=>0,'completed'=>0),
'45-49'=>array('total'=>0,'completed'=>0),
'50-54'=>array('total'=>0,'completed'=>0),
'55-59'=>array('total'=>0,'completed'=>0)
),"race"=>array(
"White"=>array('total'=>0,'completed'=>0),
"Black"=>array('total'=>0,'completed'=>0),
"Hispanic"=>array('total'=>0,'completed'=>0),
"Asian"=>array('total'=>0,'completed'=>0),
"Pacific Islander"=>array('total'=>0,'completed'=>0),
"Multiracial"=>array('total'=>0,'completed'=>0),
"Other"=>array('total'=>0,'completed'=>0)
)
有干净的方法吗?
谢谢!
答案 0 :(得分:4)
list($min,$max) = explode('-', $array[$i]);
if ($mysql[$i] >= $min && $mysql[$i] <= $max) ...
答案 1 :(得分:1)
PHP的range() function可能有用:
foreach ($array['age'] as $ageRange => $something) {
list($start, $limit) = explode('-', $ageRange);
foreach (range($start, $limit) as $age) {
// compare
}
}