如何将值从一个数组附加到相同键的另一个数组?

时间:2013-12-21 09:34:36

标签: php

我有两个数组

第一

Array
(
    [45712] => En Vogue Queen Upholstered Bed == 1675
    [45713] => En Vogue  Nightstand == 1315
    [45714] => En Vogue  Drawer Dresser == 1719
    [45715] => En Vogue  Mirror == 1225
    [45716] => En Vogue  Floor Mirror  == 1349
)

第二

Array
(
    [45712] => SLF-8480-252-256-508 == 23775
    [45713] => SLF-8480-050 == 17221
    [45714] => SLF-8480-010 == 17217
    [45715] => SLF-8480-030 == 17218
    [45716] => SLF-8480-037 == 17219
)

我想输出

Array
(
    [45712] => SLF-8480-252-256-508 == 23775 En Vogue Queen Upholstered Bed == 1675
    [45713] => SLF-8480-050 == 17221 == En Vogue  Nightstand == 1315
    [45714] => SLF-8480-010 == 17217 == En Vogue  Drawer Dresser == 1719
    [45715] => SLF-8480-030 == 17218 == Mirror == 1225
    [45716] => SLF-8480-037 == 17219 == Floor Mirror  == 1349
)

我怎样才能做到这一点?

4 个答案:

答案 0 :(得分:2)

<?php

$array1 = array(
    45712 => "En Vogue Queen Upholstered Bed == 1675",
    45713 => "En Vogue  Nightstand == 1315",
    45714 => "En Vogue  Drawer Dresser == 1719",
    45715 => "En Vogue  Mirror == 1225",
    45716 => "En Vogue  Floor Mirror  == 1349"
);
$array2 = array(
    45712 => "SLF-8480-252-256-508 == 23775",
    45713 => "SLF-8480-050 == 17221",
    45714 => "SLF-8480-010 == 17217",
    45715 => "SLF-8480-030 == 17218",
    45716 => "SLF-8480-037 == 17219"
);

foreach ($array1 as $key => $value) {
    if ($array1[$key] == $array1[$key]) {
        $array3[] = $array1[$key] . " == " . $array2[$key];
    }
}
print_r($array3);
?>

输出:http://codepad.org/91f3NFEr

答案 1 :(得分:1)

这样的事情会起作用。

$new_arr = array();
foreach($yoursecondarr as $k=$v)
{
$new_arr[$k]=$v." ".$yourfirstarr[$k];
}
print_r($new_arr);

答案 2 :(得分:1)

$array1 = array(45712 => "En Vogue Queen Upholstered Bed == 1675", 45713 => "En Vogue  Nightstand == 1315", 45714 => "En Vogue  Drawer Dresser == 1719", 45715 => "En Vogue  Mirror == 1225", 45716 => "En Vogue  Floor Mirror  == 1349");
$array2 = array(45712 => "SLF-8480-252-256-508 == 23775", 45713 => "SLF-8480-050 == 17221", 45714 => "SLF-8480-010 == 17217", 45715 => "SLF-8480-030 == 17218", 45716 => "SLF-8480-037 == 17219");

foreach ($array2 as $key => $value){
    $array2[$key] = $value." == ".$array1[$key];
}

print_r($array2);

输出:

Array
(
    [45712] => SLF-8480-252-256-508 == 23775 == En Vogue Queen Upholstered Bed == 1675
    [45713] => SLF-8480-050 == 17221 == En Vogue  Nightstand == 1315
    [45714] => SLF-8480-010 == 17217 == En Vogue  Drawer Dresser == 1719
    [45715] => SLF-8480-030 == 17218 == En Vogue  Mirror == 1225
    [45716] => SLF-8480-037 == 17219 == En Vogue  Floor Mirror  == 1349
)

答案 3 :(得分:1)

您可以使用以下代码:

foreach ($first_array as $key => $value) {
  if (isset($second_array[$key]) {
    $second_array[$key] = ' ' . $value; 
  } else {
    $second_array[$key] = $value;
  }
}
print_r($second_array);