How can I merge two arrays (one with string => value pairs and another with int => value pairs) while keeping the string/int keys? None of them will ever overlap (because one has only strings and the other has only integers).

以下是我当前的代码(这不起作用,因为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相关问答推荐

获取非重复记录的最大值

如何在php中更新ajax成功的子div元素

WooCommerce 获取产品属性分类值

弹性豆茎与 laravel 特使

使用关联数组

laravel Passport 身份验证代码 - 要求授予权限,是否需要?

获取有帖子的用户

Laravel 使用参数创建自定义异常并以 JSON 形式返回

Laravel,sync() - 如何同步数组并传递额外的数据透视字段?

为什么我在创建新项目时会收到​​ laravel 错误?

如何在 Mac OSX 上轻松切换 PHP 版本?

为什么我在引入属性类型提示时突然收到“在初始化之前不能访问类型属性”错误?

Google reCaptcha 响应“未捕获(promise )为空”

如何更新使用已弃用 each() 函数的代码?

Laravel:PDOException:找不到驱动程序

composer :file_put_contents(./composer.json):无法打开流:权限被拒绝

有没有办法在 PHP 中扩展特征?

artisan 迁移错误“找不到类'Doctrine\\DBAL\\Driver\\PDOMySql\\Driver'”,

使用 Postman 通过原始 JSON 发送 POST 数据

PHP - 无法打开流:没有这样的文件或目录