添加新密钥& PHP中的关联数组的值?

时间:2015-01-26 13:15:13

标签: php arrays multidimensional-array

我正在建立一个网络刮刀来检查价格...所以我有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
 ) 
) 

另一个名为富有想象力的阵列:$ results2:

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

我无法弄清楚如何做到最好。或者,如果我在获取数据(对于$ results2数组)时在foreach循环中将新键和值添加到原始$ results数组会更好,所以我不需要第二个阵列?

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

尝试使用此功能。 。

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