如何在保留字符串/整数键的同时合并两个数组(一个带有字符串 => 值对,另一个带有 int => 值对)?它们都不会重叠(因为一个只有字符串,另一个只有整数)。
这是我当前的代码(它不起作用,因为 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,
)
关于PHP:合并两个数组,同时保留键而不是重新索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3292044/