无法在PHP中连接2个数组

Can't concatenate 2 arrays in PHP

我最近学习了如何使用PHP中的+操作符连接2个数组。

但是考虑一下这个代码…

1
2
3
4
5
$array = array('Item 1');

$array += array('Item 2');

var_dump($array);

输出是

array(1) { [0]=> string(6)"Item
1" }

为什么这不起作用?跳过速记并使用$array = $array + array('Item 2')也不起作用。这跟钥匙有关系吗?


(P)Both will have a key of EDOCX1,and that method of combining the arrays will collapse duplicates.Try using EDOCX1第二个音标(p)字母名称(P)如果你在不同的基础上使用的要素,那么"EDOCX1"的第3个音节(p)字母名称(P)Edit:Added a code snippet to clarify(p)


(P)使用字母名称See the documentation here:http://php.net/manual/en/function.array-merge.php(p)布尔奇1


(P)所有的问题都是错的!Merge actually merges the arrays,meaning,if the arrays have a common item one of the copies will be omitted.跟我一起去吧(p)(P)I did't find a"work-around"for this issue,but to actually do i t manually…(p)(P)就在这里(p)字母名称


(P)EDOCX1 Censo 3 is called the Union operator,which differs from a concateration operator(php doesn't have one for arrays).The description clearly says:(p)布尔奇1(P)With the example:(p)字母名称(P)既然你的阵营里有一个带着一个字母拼写的字母表,结果是预料之中的。(p)(P)联系,使用字母名称7。(p)


(P)Try Array U Merge。(p)字母名称(P)I think its because you a re not assigning a key to either,so they both have key of 0,and the+does not re-index,so its trying to over write it.(p)


(P)It is indeed a key conflict.当连结结结结结时,双倍的钥匙不是万能的。(p)(P)Instead you must use EDOCX1(p)字母名称


字母名称(P)或(p)字母名称


(P)这项工作是为非联合社团:(p)(P)字母名称(p)


(P)Try Saying(p)字母名称(P)Although it looks like you're trying to add an array into an array,thus EDOCX1 penogical 0.But that's not what your title suggests.(p)


(P)你可以用操作员。Array3$Array1.Array2;(p)