【问题标题】:Compare 2 multi dimension array and update old array to latest value in PHP [closed]比较2个多维数组并将旧数组更新为PHP中的最新值[关闭]
【发布时间】:2021-05-01 10:29:51
【问题描述】:

我需要你的 PHP 帮助 我有 2 个数组。 $old 数组有 5000 条记录 示例:

$old = Array
 (
    'branch' => Array
        (

            '1' => Array
                (
                    'slug' => 'script',
                    'version' => '1.0.0',
                   'link' => 'oldfile'
                ),

            '2' => Array
                (
                    'slug' => 'pos',
                    'version' => '2.4.0',
                    'link' => 'oldfile'
                ),

            '3' => Array
                (
                    'slug' => 'stock',
                    'version' => '5.0.0',
                    'link' => 'oldfile'
                 
                ),

            '4' => Array
                (
                    'slug' => 'sale',
                    'version' => '22.0.2',
                    'link' => 'oldfile'
                    
                ),
        )
);

新数组不多,可能只有 10-30 条记录

$new = Array
(
    'branch' =>  Array  
         (

            '1' => Array
                (
                    'slug' => 'script',
                    'version' => '1.23.0',
                   'link' => 'newfile'
                ),

            '2' => Array
                (
                    'slug' => 'stock',
                    'version' => '5.9.0',
                    'link' => 'newfile'
                 
                ),
     )
);

我想比较旧数组和新数组的版本 并希望通过更改为新版本和链接来更新旧数组中的数据 示例新数组有可用的新版本和新的下载链接。

我需要得到这个结果的函数

$old = Array
 (
    'branch' => Array
        (

            '1' => Array
                (
                    'slug' => 'script',
                    'version' => '1.23.0',
                   'link' => 'newfile'
                ),

            '2' => Array
                (
                    'slug' => 'pos',
                    'version' => '2.4.0',
                    'link' => 'oldfile'
                ),

            '3' => Array
                (
                    'slug' => 'stock',
                    'version' => '5.9.0',
                    'link' => 'newfile'
                 
                ),

            '4' => Array
                (
                    'slug' => 'sale',
                    'version' => '22.0.2',
                    'link' => 'oldfile'
                    
                ),
        )
);

我正在使用

$result = array_replace_recursive($old, $new);

但它将全部替换为旧文件

【问题讨论】:

  • 欢迎来到 Stack Overflow。虽然我们很乐意在您遇到困难时提供帮助,但您仍然需要自己努力找到解决方案。如果您写了一些东西,请编辑您的问题以显示您的代码并解释它在哪里以及如何准确地无法满足您的需求。

标签: php multidimensional-array compare


【解决方案1】:

如果在创建新数组时保持一致的索引,一个简单的 array_replace 就可以解决问题

所以我的解决方案在这里找到新旧之间的共同 slug,然后在替换之前将它们从旧的中删除。假设“slug”键始终存在并且标记为相同,新数组也不能为空。因此没有检查礼物。

foreach($new['branch'] as $arrwithin){ //get arrays within the new multidim array

 foreach($old['branch'] as $key => $value){
    
  if($value['slug'] == $arrwithin['slug']){ //check if the same slugs in new array are present in the old array 

            $del[] = $key; //Get index of the old arrays within multidim where similar slugs are found as a new array

                                           }  
      } 

}

$oldclean = array_diff_key($old['branch'], array_flip($del)); //Remove the arrays which contain old slugs that have been updated in new array
$final['branch'] = array_merge($new['branch'],$oldclean); //Merge old cleaned up array with new array since well unwanted slugs are already removed

echo "<pre>";
print_r($final); //New multidim array with updated arrays 
echo "</pre>";

适用于 PHP 5.1 及更高版本(在 PHP 8 上测试)

【讨论】:

  • 非常感谢@kevin
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多