结合了两种阵列,数字键,而不会覆盖旧密钥 [英] Combine Two Arrays with numerical keys without overwriting the old keys

查看:87
本文介绍了结合了两种阵列,数字键,而不会覆盖旧密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不希望使用array_merge()<击>,因为它导致了我误解了相同的按键所有值将被覆盖。我有两个数组

  $ ARRAY1 =阵列(0 =&GT;'富',1 =&GT;'巴');
$数组2 =阵列(0 =&GT;'酒吧',1 =&GT;'富');

,并想将它们组合起来产生这样的

 阵列(0 =&GT;'富',1 =&GT;'酒吧',2 =&GT;'酒吧',3 =&GT;'富');


解决方案

array_merge() 追加的第二个数组的第一个值。它不会覆盖按键。

您的例子,结果是:


  

阵列(
      [0] =>富
      [1] =>酒吧
      [2] =>酒吧
      [3] =>富)


然而,如果输入的数组中有相同的字符串键,那么该键的后面的值将覆盖previous之一。然而,如果数组包含数字键,后面的值将不会覆盖原来的值,而是会被追加。

除非这只是一个例子,你有另一个问题?

I don't want to use array_merge() as it results in i misunderstood that all values with the same keys would be overwritten. i have two arrays

$array1 = array(0=>'foo', 1=>'bar');
$array2 = array(0=>'bar', 1=>'foo');

and would like to combine them resulting like this

array(0=>'foo', 1=>'bar',2=>'bar', 3=>'foo');

解决方案

array_merge() appends the values of the second array to the first. It does not overwrite keys.

Your example, results in:

Array ( [0] => foo [1] => bar [2] => bar [3] => foo )

However, If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

Unless this was just an example to another problem you were having?

这篇关于结合了两种阵列,数字键,而不会覆盖旧密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆