首页 \ 问答 \ 优化算法以避免夸大的脚本执行时间(Optimizing algorithm to avoid exaggerated script execution time)

优化算法以避免夸大的脚本执行时间(Optimizing algorithm to avoid exaggerated script execution time)

昨天我问这里通过键比较将数组值附加到另一个数组 ,我尝试了一些没有成功的事情。 我有两个数组$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]共享相同的密钥,对吧? 好的,那么我需要的是在$result[1]的末尾推送$result1[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


Yesterday I ask here Append array values to another array by keys comparison and I have tried a few things without success. I have two arrays $result and $result1. count($result) is 204640 and count($result1) is 129849 so they are huge. The arrays is the result of a PDO statement execution as shown in code below. This is an example for $result array:

'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',
      )

And this is an example for $result1 array:

'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',
      ),

As you can see $result[1] and $result1[1] shares the same keys, right? Ok, then what I need is to push the content of $result1[1] on the end of $result[1] and getting something like:

'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',
      )

Meaning when keys are equal in both arrays then merge or append values from the second one into the first one. Right now my best approach is the following:

// 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);

But it takes an eternity due to arrays length, so any advice in how to speedup this?

UPDATE: almost a solution

Based on @decese solution I have rewrite this a bit so my apologies in first and take a look to this. Now I called arrays as $oneArr and $twoArr and also I write a small output examples (don't kill me just yet):

// 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',
      )

This is the ouput I want to achieve, meaning when keys are equal in both arrays then merge or append values from the second one into the first one:

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

I have this done, again based on first answer which I thanks a lot for clear a bit things to me, with the following code:

// 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);

But result is not the same as I want since I got this:

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

How I can avoid the extra array on the result array?

PS: Times looks good now: Call time was 00:01:34


原文:
更新时间:2022-04-14 19:04

最满意答案

使用ControlTemplate而不是DataTemplate

 <ControlTemplate  x:Key="TabContentPresenter">
        <Border Margin="10" 
                    CornerRadius="8" 
                    BorderThickness="2" 
                    Grid.Row="0" 
                    Padding="5"  
                    Background="{TemplateBinding Background}">
            <ContentPresenter Content="{Binding}"/>
        </Border>
    </ControlTemplate>

使用模板而不是ContentTemplate

<ContentControl  Background="Green" Template="{StaticResource  TabContentPresenter}"/>

Use ControlTemplate instead DataTemplate

 <ControlTemplate  x:Key="TabContentPresenter">
        <Border Margin="10" 
                    CornerRadius="8" 
                    BorderThickness="2" 
                    Grid.Row="0" 
                    Padding="5"  
                    Background="{TemplateBinding Background}">
            <ContentPresenter Content="{Binding}"/>
        </Border>
    </ControlTemplate>

Use Template instead of ContentTemplate

<ContentControl  Background="Green" Template="{StaticResource  TabContentPresenter}"/>

相关问答

更多
  • 三个DataTriggers应该做到这一点