PHP:合并两个数组,同时保持键而不是reindexing?

我怎样才能合并两个数组(一个string=>值对,另一个与int =>值对),同时保持string/ int键? 他们都不会重叠(因为一个只有string,另一个只有整数)。

这是我目前的代码(这是行不通的,因为array_merge是用整数键重新索引数组)。

// get all id vars by combining the static and dynamic $staticIdentifications = array( Users::userID => "USERID", Users::username => "USERNAME" ); // get the dynamic vars, formatted: varID => varName $companyVarIdentifications = CompanyVars::getIdentificationVarsFriendly($_SESSION['companyID']); // merge the static and dynamic vars (*** BUT KEEP THE INT INDICES ***) $idVars = array_merge($staticIdentifications, $companyVarIdentifications); 

你可以简单地“添加”数组:

 >> $a = array(1, 2, 3); array ( 0 => 1, 1 => 2, 2 => 3, ) >> $b = array("a" => 1, "b" => 2, "c" => 3) array ( 'a' => 1, 'b' => 2, 'c' => 3, ) >> $a + $b array ( 0 => 1, 1 => 2, 2 => 3, 'a' => 1, 'b' => 2, 'c' => 3, ) 

考虑到你有

 $replaced = array('1' => 'value1', '4' => 'value4'); $replacement = array('4' => 'value2', '6' => 'value3'); 

$merge = $replacement + $replaced; 会输出:

 Array('1' => 'value1', '4' => 'value2', '6' => 'value3'); 

sum中的第一个数组将在最终输出中具有值。

$merge = $replaced + $replacement; 会输出:

 Array('1' => 'value1', '4' => 'value4', '6' => 'value3');