合并两个数组而不更改键值php

Gan*_*abu 8 php arrays

我在php中有两个数组,如代码所示

<?php
$a=array('0'=>array('500'=>'1','502'=>'2'));
$b=array('0'=>array('503'=>'3','504'=>'5'));
print_r(array_merge($a[0],$b[0]));
?>
Run Code Online (Sandbox Code Playgroud)

我需要合并两个数组.array_merge函数成功合并了其中两个,但键值发生了变化.我需要以下输出

  Array
    (
        [0]=>Array(
           [500] => 1
           [502] => 2
           [503] => 3
           [504] => 5
         )
    )
Run Code Online (Sandbox Code Playgroud)

我可以在php中使用什么功能,以便在不更改键值的情况下获得以下输出?

lc.*_*lc. 17

从文档中,示例#3:

如果要将第二个数组中的数组元素追加到第一个数组而不覆盖第一个数组中的元素而不重新编制索引,请使用+ array union运算符:

<?php
$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);
?>
Run Code Online (Sandbox Code Playgroud)

将保留第一个数组中的键.如果两个数组中都存在数组键,则将使用第一个数组中的元素,并忽略第二个数组中的匹配键元素.

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"
}
Run Code Online (Sandbox Code Playgroud)

因此,尝试: $a[0] + $b[0]


Ber*_*ere 6

$a=array('0'=>array('500'=>'1','502'=>'2'));
$b=array('0'=>array('503'=>'3','504'=>'5'));

$c = $a + $b; //$c will be a merged array
Run Code Online (Sandbox Code Playgroud)

查看这个问题的答案