在PHP中过滤数据数据的有效方法

时间:2014-03-14 16:43:46

标签: php api reporting core

我正在尝试过滤这两个数组。

第一个数组($posters)有大约600条记录:$ poster = array

Array
(
    [0] => Array
        (
            [Poster_ID] => 8
            [Brand] => xyz
            [Therapy] => xyz
            [Agency] => xyz 
            [Client_Approval] => 1
            [Poster_Number] => ...
            [Identifier] => 517035
            [Presentation_Date] => 2011-11-15 00:00:00
            [Expiry_Date] => 2012-11-14 00:00:00

        )

    [1] => Array
        (
            [Poster_ID] => 8
            [Brand] => xyz
            [Therapy] => xyz
            [Agency] => xyz 
            [Client_Approval] => 1
            [Poster_Number] => ...
            [Identifier] => 517035
            [Presentation_Date] => 2011-11-15 00:00:00
            [Expiry_Date] => 2012-11-14 00:00:00

        )

    [2] => Array
        (
            [Poster_ID] => 5
            [Brand] => Lialda
            [Therapy] => 
            [Agency] => MedErgy Scientific
            [Client_Approval] => 1
            [Poster_Number] => P173
            [Identifier] => 444909
            [Presentation_Date] => 2012-02-17 00:00:00
            [Expiry_Date] => 2012-03-18 01:00:00

        ) up to 600. These data come from database

另一个数组($stats_result)至少有来自Google Analytics(分析)帐户的9000条记录,如下所示:

 [0] => Array
            (
                [0] => 20120126
                [1] => /249441
                [2] => 1
                [3] => 2
                [4] => 1
            )

        [1] => Array
            (
                [0] => 20120126
                [1] => /249441/pdf-emailed.php
                [2] => 1
                [3] => 1
                [4] => 1
            )
        [2] => Array
            (
                [0] => 20120127
                [1] => /517035
                [2] => 1
                [3] => 2
                [4] => 1
            )

        [3] => Array
            (
                [0] => 20120129
                [1] => /517035
                [2] => 1
                [3] => 1
                [4] => 1
            )
        [4] => Array
            (
                [0] => 20130503
                [1] => /257388/email_sent/PT
                [2] => 1
                [3] => 6
                [4] => 1
            )
        [5] => Array
            (
                [0] => 20130606
                [1] => /417202/email_sent/UK
                [2] => 1
                [3] => 1
                [4] => 1
            )
        [6] => Array
            (
                [0] => 20130410
                [1] => http://www.xyz.com/uploads/France/819412/download/FR.pdf
                [2] => 1
                [3] => 9
                [4] => 1
            )
        [7] => Array
            (
                [0] => 20130411
                [1] => http://www.xyz.com/uploads/149096.pdf
                [2] => 1
                [3] => 5
                [4] => 1
            )
    .....

从Google解析中检索此数据后,我将循环结果并将其存储到单独的数组中:

for ($i=0; $i < count($stats_result); $i++) { 
    $stats_results[]=array(
        'path' => $stats_result[$i][1],
        'start_date' => $stats_result[$i][0],
        'visitors'=>$stats_result[$i][2],
        'emails'=>$stats_result[$i][4],
        'downloads'=>$stats_result[$i][4]
    );
}

然后我使用这个lambda函数来过滤这两个大数组,如下所示。此函数检查海报数组中的$poster['Idetifier']是否存在于第二个数组中(如果存在),然后检查日期。

foreach ($posters as &$poster)
                {
                $stats[] = 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);
                    }
                    }

最后我根据下面的路径添加它们;

$finalStats = array();

            foreach ($stats as $key => $value) {
                foreach ($value as $stat){
                    if(isset($finalStats[$stat['path']])){
                        $finalStats[$stat['path']]['visitors'] += intval($stat['visitors']);
                        $finalStats[$stat['path']]['emails'] += intval($stat['emails']);
                        $finalStats[$stat['path']]['downloads'] += intval($stat['downloads']);
                    }else{
                        $finalStats[$stat['path']]['visitors'] = intval($stat['visitors']);
                        $finalStats[$stat['path']]['emails'] = intval($stat['emails']);
                        $finalStats[$stat['path']]['downloads'] = intval($stat['downloads']);
                    }
                }
            }

这一切都按预期工作。但无论如何我可以让这些过程更快。我首先尝试了isset()函数,但执行需要很多时间。现在我已经尝试过lambda函数,但它仍然需要花费更多的时间来执行。有没有其他方法可以过滤这些数据。非常感谢你提前。需要认真帮助。

0 个答案:

没有答案