0

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

$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' );

谢谢!

4

3 回答 3

2

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

$resultArray = array_merge($targetArray,$sourceArray);
于 2012-07-19T18:12:19.107 回答
1

使用数组合并:

$merged = array_merge($targetArray, $sourceArray);
// will result array('a'=>'a','b'=>'','c'=>'c','d'=>'d');
于 2012-07-19T18:12:22.257 回答
1

使用array_merge()

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

输出

array(4) {
  ["a"]=>
  string(1) "a"
  ["b"]=>
  string(0) ""
  ["c"]=>
  string(1) "c"
  ["d"]=>
  string(1) "d"
}
于 2012-07-19T18:12:33.783 回答