我有一个大型数组,我也根据路径和日期过滤了数组。这是我为了做到这一点而编写的代码,但由于数组的大小,执行时间有点太长。有没有其他方法可以更有效地做到这一点。
foreach ($posters as &$poster) {
$stats[$poster['Identifier']] = array_filter($stats_results, function($item) use ($poster){
$date = strtotime($item['start_date']);
$presentation_date = strtotime($poster['Presentation_Date']);
$expiry=strtotime($poster['Expiry_Date']);
if($item['path'] == '/' . $poster['Identifier']){
return
($date >= $presentation_date && $date <= $expiry );
}
if($item['path'] == '/' . $poster['Identifier'].'/pdf-emailed.php'){
return
($date >= $presentation_date && $date <= $expiry);
}
if($item['path'] == 'http://www.xyz.com/uploads/' . $poster['Identifier'].'.pdf'){
return
($date >= $presentation_date && $date <= $expiry);
}
if($item['path'] == '/'.$poster['Identifier'].'/email_sent/DE'){
return
($date >= $presentation_date && $date <= $expiry);
}
if($item['path'] == '/'.$poster['Identifier'].'/email_sent/FR'){
return
($date >= $presentation_date && $date <= $expiry);
}
if($item['path'] == '/'.$poster['Identifier'].'/email_sent/ES'){
return
($date >= $presentation_date && $date <= $expiry);
}
if($item['path'] == '/'.$poster['Identifier'].'/email_sent/PT'){
return
($date >= $presentation_date && $date <= $expiry);
}
if($item['path'] == '/'.$poster['Identifier'].'/email_sent/IT'){
return
($date >= $presentation_date && $date <= $expiry);
}
if($item['path'] == '/'.$poster['Identifier'].'/email_sent/UK'){
return
($date >= $presentation_date && $date<= $expiry);
}
if($item['path'] == 'http://www.xyz.com/uploads/Germany/'.$poster['Identifier'].'/download/DE.pdf'){
return
($date >= $presentation_date && $date<= $expiry);
}
if($item['path'] == 'http://www.xyz.com/uploads/France/'.$poster['Identifier'].'/download/FR.pdf'){
return
($date >= $presentation_date && $date <= $expiry);
}
if($item['path'] == 'http://www.xyz.com/uploads/Spain/'.$poster['Identifier'].'/download/ES.pdf'){
return
($date >= $presentation_date && $date <= $expiry);
}
if($item['path'] == 'http://www.xyz.com/uploads/Japan/'.$poster['Identifier'].'/download/JA.pdf'){
return
($date >= $presentation_date && $date <= $expiry);
}
if($item['path'] == 'http://www.xyz.com/uploads/Portugal/'.$poster['Identifier'].'/download/PT.pdf'){
return
($date >= $presentation_date && $date <= $expiry);
}
if($item['path'] == 'http://www.xyz.com/uploads/Italy/'.$poster['Identifier'].'/download/IT.pdf'){
return
($date >= $presentation_date && $date <= $expiry);
}
if($item['path'] == 'http://www.xyz.com/uploads/'.$poster['Identifier'].'/download/UK.pdf'){
return
($date >= $presentation_date && $date <= $expiry);
}
}
提前致谢。
答案 0 :(得分:3)
创建一个数组,其中所有测试值都是键,并测试匹配。
foreach ($posters as &$poster) {
$stats[$poster['Identifier']] = array_filter($stats_results, function($item) use ($poster){
$date = strtotime($item['start_date']);
$presentation_date = strtotime($poster['Presentation_Date']);
$expiry=strtotime($poster['Expiry_Date']);
$test_array = array ('/' . $poster['Identifier'] => true
'/' . $poster['Identifier'].'/pdf-emailed.php' => true
'http://www.xyz.com/uploads/' . $poster['Identifier'].'.pdf' => true
'/'.$poster['Identifier'].'/email_sent/DE' => true
'/'.$poster['Identifier'].'/email_sent/FR' => true
'/'.$poster['Identifier'].'/email_sent/ES' => true
'/'.$poster['Identifier'].'/email_sent/PT' => true
'/'.$poster['Identifier'].'/email_sent/IT' => true
'/'.$poster['Identifier'].'/email_sent/UK' => true
'http://www.xyz.com/uploads/Germany/'.$poster['Identifier'].'/download/DE.pdf' => true
'http://www.xyz.com/uploads/France/'.$poster['Identifier'].'/download/FR.pdf' => true
'http://www.xyz.com/uploads/Spain/'.$poster['Identifier'].'/download/ES.pdf' => true
'http://www.xyz.com/uploads/Japan/'.$poster['Identifier'].'/download/JA.pdf' => true
'http://www.xyz.com/uploads/Portugal/'.$poster['Identifier'].'/download/PT.pdf' => true
'http://www.xyz.com/uploads/Italy/'.$poster['Identifier'].'/download/IT.pdf' => true
'http://www.xyz.com/uploads/'.$poster['Identifier'].'/download/UK.pdf' => true);
if (isset($test_array[$item['path']])) {
return ($date >= $presentation_date && $date <= $expiry);
}
});
}
其他可能性是使用switch
语句。或者,由于您的许多字符串非常相似,您可以将它们组合成具有替换的正则表达式,并使用preg_match
。