Reputation: 42
I have a large array and I have too filter the array based on path and date. Here is the code I have written in order to do that but it is taking a bit too long to execute because the size of the array. Is there any other way I can do this more efficiently.
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);
}
}
Thanks in advance.
Upvotes: 0
Views: 59
Reputation: 781721
Create an array where all the test values are keys, and test for a match.
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);
}
});
}
Other possibilities are to use a switch
statement. Or, since many of your strings are very similar, you can combine them into a regular expression with alternations, and use preg_match
.
Upvotes: 3