0

想合并

array(
 2 => 'a'
 3 => 'b'
);

array(
 7 => 'c'
 9 => 'd'
);

要得到

array(
 2 => 'a'
 3 => 'b'
 7 => 'c'
 9 => 'd'
);

array_merge()给我

array(
 0 => 'a'
 1 => 'b'
 2 => 'c'
 3 => 'd'
);
4

4 回答 4

6

很简单,你可以这样做-

$result = $array1 + $array2;

简单的例子

$array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a');
$array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b');
$result = $array1 + $array2;
var_dump($result);

输出

array(5) {
  [0]=>
  string(6) "zero_a"
  [2]=>
  string(5) "two_a"
  [3]=>
  string(7) "three_a"
  [1]=>
  string(5) "one_b"
  [4]=>
  string(6) "four_b"
}
于 2013-01-10T13:07:35.573 回答
0

您可以简单地添加数组:$myArray1 + $myArray2;

于 2013-01-10T13:09:35.117 回答
0

使用数组统一运算符:“+”

<?php

$foo = array(
 2 => 'a',
 3 => 'b'
);

$bar = array(
 7 => 'c',
 9 => 'd'
);

$result = $foo + $bar;

print_r( $result );
于 2013-01-10T13:11:43.617 回答
0

$阵列1 + $阵列2;

是我的选择,而且效果很好。

于 2013-01-10T13:15:31.887 回答