比较2个多维数组并将旧数组更新为PHP中的最新值
我需要你在PHP方面的帮助 我有两个数组。 $old数组有5000条记录 例如: 新阵列不多,可能有10-30条记录比较2个多维数组并将旧数组更新为PHP中的最新值,php,multidimensional-array,compare,Php,Multidimensional Array,Compare,我需要你在PHP方面的帮助 我有两个数组。 $old数组有5000条记录 例如: 新阵列不多,可能有10-30条记录 $new = Array ( 'branch' => Array ( '1' => Array ( 'slug' => 'script', 'version' => '1.23.0',
$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);
但它会将所有文件替换为旧文件如果在创建新数组时保持一致的索引,那么简单的数组替换就可以解决这个问题 因此,我在这里的解决方案是在新旧数组之间找到共同的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>";
foreach($new['branch']as$arrinsin){//获取新多维数组中的数组
foreach($old['branch']作为$key=>$value){
如果($value['slug']==$arrinsin['slug']){//检查新数组中的相同slug是否存在于旧数组中
$del[]=$key;//获取multidim中旧数组的索引,其中类似的slug作为新数组找到
}
}
}
$oldclean=array_diff_key($old['branch'],array_flip($del))//删除包含已在新阵列中更新的旧段塞的阵列
$final['branch']=数组合并($new['branch'],$oldclean)//将旧的清理阵列与新阵列合并,因为不需要的井段塞已被移除
回声“;
打印(最终版)//具有更新阵列的新多维阵列
回声“;
适用于PHP5.1及以上版本(在PHP8上测试)欢迎使用堆栈溢出。虽然我们很乐意在您陷入困境时提供帮助,但我们仍然希望您自己努力找到解决方案。如果你已经写了一些东西,请编辑你的问题以显示你的代码,并解释它在哪里以及如何无法满足你的需要。非常感谢@kevin
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>";