PHP:合并两个数组同时保留键而不是重新索引?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3292044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:15:17  来源:igfitidea点击:

PHP: merge two arrays while keeping keys instead of reindexing?

phparraysarray-merge

提问by Garrett

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).

如何在保留 string/int 键的同时合并两个数组(一个带有字符串 => 值对,另一个带有 int => 值对)?它们都不会重叠(因为一个只有字符串,另一个只有整数)。

Here is my current code (which doesn't work, because array_merge is re-indexing the array with integer keys):

这是我当前的代码(它不起作用,因为 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);

回答by SirDarius

You can simply 'add' the arrays:

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

>> $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,
)

回答by CRK

Considering that you have

考虑到你有

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

Doing $merge = $replacement + $replaced;will output:

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

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

The first array from sum will have values in the final output.

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

Doing $merge = $replaced + $replacement;will output:

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

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

回答by danopz

While this question is quite old I just want to add another possibility of doing a merge while keeping keys.

虽然这个问题很老了,但我只想添加另一种在保留密钥的同时进行合并的可能性。

Besides adding key/values to existing arrays using the +sign you could do an array_replace.

除了使用+符号将键/值添加到现有数组之外,您还可以执行array_replace.

$a = array('foo' => 'bar', 'some' => 'string');
$b = array(42 => 'answer to the life and everything', 1337 => 'leet');

$merged = array_replace($a, $b);

The result will be:

结果将是:

Array
(
  [foo] => bar
  [some] => string
  [42] => answer to the life and everything
  [1337] => leet
)

Same keys will be overwritten by the latter array.
There is also an array_replace_recursive, which do this for subarrays, too.

相同的键将被后一个数组覆盖。
还有一个array_replace_recursive,它也对子数组执行此操作。

Live example on 3v4l.org

3v4l.org 上的实时示例

回答by amba patel

Two arrays can be easily added or union without chaning their original indexing by +operator. This will be very help full in laravel and codeigniter select dropdown.

两个数组可以很容易地添加或合并,而无需通过+运算符更改它们的原始索引。这将非常有助于 laravel 和 codeigniter 选择下拉列表。

 $empty_option = array(
         ''=>'Select Option'
          );

 $option_list = array(
          1=>'Red',
          2=>'White',
          3=>'Green',
         );

  $arr_option = $empty_option + $option_list;

Output will be :

输出将是:

$arr_option = array(
   ''=>'Select Option'
   1=>'Red',
   2=>'White',
   3=>'Green',
 );

回答by zdenek

Try array_replace_recursive or array_replace functions

尝试 array_replace_recursive 或 array_replace 函数

$a = array('userID' => 1, 'username'=> 2);
array (
  userID => 1,
  username => 2
)
$b = array('userID' => 1, 'companyID' => 3);
array (
  'userID' => 1,
  'companyID' => 3
)
$c = array_replace_recursive($a,$b);
array (
  userID => 1,
  username => 2,
  companyID => 3
)

http://php.net/manual/en/function.array-replace-recursive.php

http://php.net/manual/en/function.array-replace-recursive.php