在 PHP 中向关联数组添加新键和值


Adding new keys & values to Associative array in PHP?

我正在构建一个网络爬虫来检查价格...所以我有 2 个数组,我正在尝试将数据合并到一个数组中。第一个数组称为$results,如下所示:

Array ( 
[0] => Array ( 
[id] => 0 
    [myTitle] => Product One text... 
    [myImage] => Some url...
    [myPrice] => My price
) 
[1] => Array ( 
[id] => 1 
    [myTitle] => Product Two text... 
    [myImage] => Some url...
    [myPrice] => My price
 ) 
) 

第二个数组富有想象力地调用:$results 2:

Array ( 
[0] => Array ( 
    [amzId] => 0 
    [amzTitle] => Amazon Product One text... 
    [amzPrice] => Amazon price
) 
[1] => Array ( 
    [amzId] => 1
    [amzTitle] => Amazon Product Two text...
    [amzPrice] => Amazon price
 )  
)

我需要将第二个数组键和值添加到第一个数组中,其中 Id 和"amzId"是相同的。

Array ( 
[0] => Array ( 
[id] => 0 
    [myTitle] => Product One text... 
    [myImage] => Some url...
    [myPrice] => My price
    [amzId] => 0 
    [amzTitle] => Amazon Product One text... 
    [amzPrice] => Amazon price
) 
[1] => Array ( 
[id] => 1 
    [myTitle] => Product Two text... 
    [myImage] => Some url...
    [myPrice] => My price
    [amzId] => 1 
    [amzTitle] => Amazon Product Two text... 
    [amzPrice] => Amazon price
 ) 
)

我不知道如何最好地做到这一点。或者,如果我在获取数据(对于 $results 2 数组)时最好在 foreach 循环中将新的键和值添加到原始$results数组中,这样我就不需要第二个数组了?

任何帮助将不胜感激!

尝试使用此函数。

$arr3 = array_merge($arr1,$arr2);