如何合并两个数组(一个带有字符串=>值对,另一个带有int =>值对),同时保留字符串/ 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, )