优化算法以避免夸大的脚本执行时间

时间:2015-08-11 14:27:35

标签: php arrays algorithm optimization

昨天我问Append array values to another array by keys comparison,我尝试了一些没有成功的事情。我有两个数组$result$result1count($result)204640count($result1)129849,因此它们非常庞大。数组是PDO语句执行的结果,如下面的代码所示。这是$result数组的示例:

'00180000015oGSWAA2' =>
    array (
        'accountId' => '00180000015oGSWAA2',
        'npi' => '1053576223',
        'firstname' => 'Jack',
        'lastname' => 'Cheng',
        'title' => '',
        'accountLastModifiedDate' => '2014-09-09 17:37:15',
        'suffix' => '',
        'fax' => '',
        'address1' => '853 N CHURCH ST',
        'city' => 'SPARTANBURG',
        'stateLicensedId' => '31191',
        'state' => 'SC',
        'phone' => '',
        'zip' => '29303',
        'address2' => '',
        'addressLastModifiedDate' => '2014-09-04 08:44:17',
      ),
'00180000015oGeXAAU' =>
    array (
        'accountId' => '00180000015oGeXAAU',
        'npi' => '1629067301',
        'firstname' => 'Fred',
        'lastname' => 'Thaler',
        'title' => '',
        'accountLastModifiedDate' => '2014-09-09 17:36:41',
        'suffix' => '',
        'fax' => '',
        'address1' => '1 PEARL ST',
        'city' => 'BROCKTON',
        'stateLicensedId' => '58249',
        'state' => 'MA',
        'phone' => '',
        'zip' => '2301',
        'address2' => '',
        'addressLastModifiedDate' => '2014-09-04 04:25:44',
      )

这是$result1数组的示例:

'001S000000nBvryIAC' =>
    array (
        'tid' => '04T800000008zySEAQ',
        'acsLastModifiedDate' => '2015-01-06 17:19:48',
      ),
'00180000015oGeXAAU' =>
    array (
        'tid' => '04T800000008zzgEAA',
        'acsLastModifiedDate' => '2015-01-07 04:06:40',
      ),
'001S000000nYWcYIAW' =>
    array (
        'tid' => '04T800000008zySEAQ',
        'acsLastModifiedDate' => '2015-02-25 15:45:01',
      ),

正如您所看到的,$result[1]$result1[1]共享相同的密钥,对吧?好的,那么我需要的是在$result1[1]的末尾推送$result[1]的内容并得到类似的内容:

'00180000015oGeXAAU' =>
    array (
        'accountId' => '00180000015oGeXAAU',
        'npi' => '1629067301',
        'firstname' => 'Fred',
        'lastname' => 'Thaler',
        'title' => '',
        'accountLastModifiedDate' => '2014-09-09 17:36:41',
        'suffix' => '',
        'fax' => '',
        'address1' => '1 PEARL ST',
        'city' => 'BROCKTON',
        'stateLicensedId' => '58249',
        'state' => 'MA',
        'phone' => '',
        'zip' => '2301',
        'address2' => '',
        'addressLastModifiedDate' => '2014-09-04 04:25:44',
        'tid' => '04T800000008zzgEAA',
        'acsLastModifiedDate' => '2015-01-07 04:06:40',
      )

当两个数组中的键相等时,然后将第二个中的值合并或附加到第一个中。现在我最好的方法是:

// PDO statement
$result = $stmt->fetchAll();

// PDO statement
$result1 = $stmt->fetchAll();

foreach ($result as $key => $row) {
    foreach ($result1 as $key1 => $row1) {
        if ($key === $key1) {
            array_push($row, array_shift($row1));
        }
    }
}

var_dump($row);

但是由于数组长度需要一个永恒,所以有关如何加速这个的建议吗?

更新:几乎是解决方案

基于 @decese 解决方案我已经重写了一下,所以我先道歉并看一看。现在我将数组调用为$oneArr$twoArr,并且我还编写了一些小输出示例(暂时不要杀了我):

// var_export($oneArr)
'00180000015oGSWAA2' =>
    array (
        'target_id' => '00180000015oGSWAA2',
        'firstname' => 'Jack',
      ),
'00180000015oGeXAAU' =>
    array (
        'target_id' => '00180000015oGeXAAU',
        'firstname' => 'Fred',
      )

// var_export($twoArr)
'001S000000nBvryIAC' =>
    array (
        'tid' => '04T800000008zySEAQ',
        'acsLastModifiedDate' => '2015-01-06 17:19:48',
      ),
'00180000015oGeXAAU' =>
    array (
        'tid' => '04T800000008zzgEAA',
        'acsLastModifiedDate' => '2015-01-07 04:06:40',
      )

这是我想要实现的输出,这意味着当两个数组中的键相等时,然后将第二个中的值合并或附加到第一个中:

'00180000015oGeXAAU' =>
    array (
        'target_id' => '00180000015oGeXAAU',
        'firstname' => 'Fred',
        'tid' => '04T800000008zzgEAA',
        'acsLastModifiedDate' => '2015-01-07 04:06:40',
      )

我已经完成了这一点,再次基于第一个答案,我非常感谢我对以下代码有所了解,并使用以下代码:

// Fetch results from first SQL query
$result = $stmt->fetchAll();

// Fetch only two keys from the entire and put them on $oneArr
foreach ($result as $row) {
    $oneArr[$row['target_id']] = [
        'target_id' => $row['target_id'],
        'firstname' => $row['firstname']
    ];
}

// Fetch results from second SQL query
// yes, var names are the same not matter, I will only use one time
$result = $stmt->fetchAll();

// Fetch only two keys from the entire and put them on $twoArr
foreach ($result as $row) {
    $twoArr[$row['target_id']] = [
        'tid'    => $row['tid'],
        'acslmd' => $row['acslmd']
    ];
}

$i = 0;
foreach ($oneArr as $keyOneArr => $valueOneArr) {
    if (array_key_exists($keyOneArr, $twoArr)) {
        array_push($oneArr[$keyOneArr], $twoArr[$keyOneArr]);
        $i++;
    }
}

var_export($oneArr);

但结果与我想要的不一样,因为我得到了这个:

'00180000015oGeXAAU' =>
    array (
        'target_id' => '00180000015oGeXAAU',
        'firstname' => 'Fred',
        0 =>
            array (
                'tid' => '04T800000008zzgEAA',
                'acslmd' => '2015-01-07 04:06:40',
        ),
    ),

如何避免结果数组上的额外数组?

PS:现在时间看起来不错:通话时间是00:01:34

2 个答案:

答案 0 :(得分:2)

看起来您的数组实际上是由ids索引的,所以这样做:

foreach ($result1 as $key => $value) {
    if (isset($result[$key])) {
        $result[$key] += $value;
    }
}

当然,如果可能的话,使用JOIN在数据库中完成所有这些工作最有意义。

答案 1 :(得分:2)

您可以使用array_merge_recursive

请参阅我的工作示例:http://3v4l.org/hQERW
注意:在提供的数据中,只有00180000015oGeXAAU对两个数组都是通用的,因此它只合并该键的值

基本上,只需致电

$result = array_merge_recursive($result, $result1);

PHP.net说:

  

array_merge_recursive()将一个或多个数组的元素合并在一起,以便将一个值的值附加到前一个数组的末尾。它返回结果数组。

     

如果输入数组具有相同的字符串键,则这些键的值将合并为一个数组,这是递归完成的,因此,如果一个值是一个数组本身,该函数也将它与另一个数组中的相应条目合并。但是,如果数组具有相同的数字键,则后面的值不会覆盖原始值,但会被追加。