PHP:合并两个数组,同时保留键而不是重新索引?
如何在保留 string/int 键的同时合并两个数组(一个包含 string => 值对,另一个包含 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);
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).
Here is my current code (which doesn't work, because array_merge is re-indexing the array with integer keys):
// 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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以简单地“添加”数组:
You can simply 'add' the arrays:
考虑到您
执行
$merge = $replacement + $replaced;
将输出:sum 中的第一个数组将在最终输出中包含值。
执行
$merge = $replaced + $replacement;
将输出:Considering that you have
Doing
$merge = $replacement + $replaced;
will output:The first array from sum will have values in the final output.
Doing
$merge = $replaced + $replacement;
will output:我只是想添加另一种在保留密钥的同时进行合并的可能性。
除了使用
+
符号将键/值添加到现有数组之外,您还可以执行array_replace
。结果将是:
相同的键将被后一个数组覆盖。
还有一个 array_replace_recursive ,它也可以对子数组执行此操作。
3v4l.org 上的实时示例
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 anarray_replace
.The result will be:
Same keys will be overwritten by the latter array.
There is also an
array_replace_recursive
, which do this for subarrays, too.Live example on 3v4l.org
可以轻松地添加或合并两个数组,而无需通过 + 运算符更改其原始索引。这对于 laravel 和 codeigniter 选择下拉列表非常有帮助。
输出将是:
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.
Output will be :
尝试 array_replace_recursive 或 array_replace 函数
http://php.net/manual/en /function.array-replace-recursive.php
Try array_replace_recursive or array_replace functions
http://php.net/manual/en/function.array-replace-recursive.php
OP.的要求是保留密钥(保留密钥)并且不重叠(我认为覆盖)。在某些情况下,例如数字键,这是可能的,但如果是字符串键,则似乎不可能。
如果您使用
array_merge()
,数字键将始终重新索引或重新编号。如果使用
array_replace()
、array_replace_recursive()
,它将从右到左重叠或覆盖。第一个数组中具有相同键的值将被第二个数组替换。如果您使用
$array1 + $array2
作为注释,如果键相同,那么它将保留第一个数组中的值,但删除第二个数组。自定义功能。
这是我刚刚编写的函数,用于满足相同的要求。您可以自由地用于任何目的。
测试。
结果。
The OP.'s requirement is to preserve keys (keep keys) and not overlap (I think overwrite). In some cases such as numeric keys it is possible but if string keys it seems to be not possible.
If you use
array_merge()
the numeric keys will always re-index or renumbered.If you use
array_replace()
,array_replace_recursive()
it will be overlap or overwrite from the right to the left. The value with the same key on first array will be replaced with second array.If you use
$array1 + $array2
as the comment was mentioned, if the keys are same then it will keep the value from first array but drop the second array.Custom function.
Here is my function that I just wrote to work on the same requirements. You are free to use for any purpose.
Testing.
The results.