将One Array中的值分配给另一个数组PHP

时间:2012-07-19 18:09:52

标签: php arrays

如何在PHP中将一个Array的值分配给另一个Array?例如,

$targetArray = array('a'=>'','b'=>'','c'=>'','d'=>'');  //array with empty value

$sourceArray = array('a'=>'a','c'=>'c','d'=>'d');       //array with value, but maybe not have all the keys of the target array

我希望看到的结果如下:     $ resultArray = array('a'=>'a','b'=>'','c'=>'c','d'=>'d');

谢谢!

3 个答案:

答案 0 :(得分:2)

我认为您正在寻找的功能是array_merge

$resultArray = array_merge($targetArray,$sourceArray);

答案 1 :(得分:1)

使用array_merge:

$merged = array_merge($targetArray, $sourceArray);
// will result array('a'=>'a','b'=>'','c'=>'c','d'=>'d');

答案 2 :(得分:1)

使用array_merge()

$targetArray = array('a'=>'','b'=>'','c'=>'','d'=>''); 
$sourceArray = array('a'=>'a','c'=>'c','d'=>'d');
$result = array_merge( $targetArray, $sourceArray);

outputs

array(4) {
  ["a"]=>
  string(1) "a"
  ["b"]=>
  string(0) ""
  ["c"]=>
  string(1) "c"
  ["d"]=>
  string(1) "d"
}