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).
如何在保留字符串/int键的同时合并两个数组(一个是string =>值对,另一个是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);
3 个解决方案
#1
443
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,
)
#2
43
Considering that you have
考虑到你有
$replaced = array('1' => 'value1', '4' => 'value4');
$replacement = array('4' => 'value2', '6' => 'value3');
Doing $merge = $replacement + $replaced;
will output:
合并= $替换+ $替换;将输出:
Array('1' => 'value1', '4' => 'value2', '6' => 'value3');
The first array from sum will have values in the final output.
sum的第一个数组将在最终输出中具有值。
Doing $merge = $replaced + $replacement;
will output:
执行$merge = $replace + $replace;将输出:
Array('1' => 'value1', '4' => 'value4', '6' => 'value3');
#3
3
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);
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,它也用于子数组。
生活例子3日v4l.org
#1
443
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,
)
#2
43
Considering that you have
考虑到你有
$replaced = array('1' => 'value1', '4' => 'value4');
$replacement = array('4' => 'value2', '6' => 'value3');
Doing $merge = $replacement + $replaced;
will output:
合并= $替换+ $替换;将输出:
Array('1' => 'value1', '4' => 'value2', '6' => 'value3');
The first array from sum will have values in the final output.
sum的第一个数组将在最终输出中具有值。
Doing $merge = $replaced + $replacement;
will output:
执行$merge = $replace + $replace;将输出:
Array('1' => 'value1', '4' => 'value4', '6' => 'value3');
#3
3
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);
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,它也用于子数组。
生活例子3日v4l.org