无法在 PHP 中连接 2 个数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2650177/
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
Can't concatenate 2 arrays in PHP
提问by alex
I've recently learned how to join 2 arrays using the + operator in PHP.
我最近学习了如何在 PHP 中使用 + 运算符连接 2 个数组。
But consider this code...
但是考虑一下这段代码......
$array = array('Item 1');
$array += array('Item 2');
var_dump($array);
Output is
输出是
array(1) { [0]=> string(6) "Item 1" }
数组(1) { [0]=> 字符串(6) "项目 1" }
Why does this not work? Skipping the shorthand and using $array = $array + array('Item 2')does not work either. Does it have something to do with the keys?
为什么这不起作用?跳过速记并使用$array = $array + array('Item 2')也不起作用。跟钥匙有关系吗?
回答by awgy
Both will have a key of 0, and that method of combining the arrays will collapse duplicates. Try using array_merge()instead.
两者都有一个键0,并且组合数组的方法将折叠重复项。尝试使用array_merge()。
$arr1 = array('foo'); // Same as array(0 => 'foo')
$arr2 = array('bar'); // Same as array(0 => 'bar')
// Will contain array('foo', 'bar');
$combined = array_merge($arr1, $arr2);
If the elements in your array used different keys, the +operator would be more appropriate.
如果数组中的元素使用不同的键,则+运算符会更合适。
$arr1 = array('one' => 'foo');
$arr2 = array('two' => 'bar');
// Will contain array('one' => 'foo', 'two' => 'bar');
$combined = $arr1 + $arr2;
Edit: Added a code snippet to clarify
编辑:添加了一个代码片段来澄清
回答by Christopher Altman
Use array_merge()
See the documentation here:
http://php.net/manual/en/function.array-merge.php
使用array_merge()
请参阅此处的文档:http:
//php.net/manual/en/function.array-merge.php
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
将一个或多个数组的元素合并在一起,以便将 1 的值附加到前一个数组的末尾。它返回结果数组。
回答by deceze
+is called the Union operator, which differs from a Concatenation operator (PHP doesn't have one for arrays). The descriptionclearly says:
+称为联合运算符,它不同于串联运算符(PHP 没有用于数组的运算符)。该说明明确表示:
The + operator appends elements of remaining keys from the right handed array to the left handed, whereas duplicated keys are NOT overwritten.
+ 运算符将右手数组中剩余键的元素附加到左手,而重复的键不会被覆盖。
With the example:
举个例子:
$a = array("a" => "apple", "b" => "banana");
$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");
$c = $a + $b;
array(3) {
["a"]=>
string(5) "apple"
["b"]=>
string(6) "banana"
["c"]=>
string(6) "cherry"
}
Since both your arrays have one entry with the key 0, the result is expected.
由于您的两个数组都有一个带有 key 的条目0,因此可以预期结果。
To concatenate, use array_merge.
要连接,请使用array_merge.
回答by alfasin
All previous answers are incorrect! mergeactually merges the arrays, meaning, if the arrays have a common item one of the copies will be omitted. Same goes for union.
之前的所有答案都不正确! merge实际上合并了数组,这意味着,如果数组有一个公共项,其中一个副本将被省略。这同样适用于工会。
I didn't find a "work-around" for this issue, but to actually do it manually...
我没有找到解决此问题的“解决方法”,但实际上手动执行...
here it goes:
它是这样的:
<?php
$part1 = array(1,2,3);
echo "array 1 = \n";
print_r($part1);
$part2 = array(4,5,6);
echo "array 2 = \n";
print_r($part2);
$ans = NULL;
for ($i = 0; $i < count($part1); $i++) {
$ans[] = $part1[$i];
}
for ($i = 0; $i < count($part2); $i++) {
$ans[] = $part2[$i];
}
echo "after arrays concatenation:\n";
print_r($ans);
?>
回答by Rabbott
Try array_merge.
尝试 array_merge。
$array1 = array('Item 1');
$array2 = array('Item 2');
$array3 = array_merge($array1, $array2);
I think its because you are 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.
我认为这是因为您没有为它们分配键,所以它们的键都是 0,并且 + 没有重新索引,所以它试图覆盖它。
回答by Brant Messenger
$array = array('Item 1');
array_push($array,'Item 2');
or
或者
$array[] = 'Item 2';
回答by Rupert Madden-Abbott
It is indeed a key conflict. When concatenating arrays, duplicate keys are not overwritten.
这确实是一个关键的冲突。连接数组时,不会覆盖重复的键。
Instead you must use array_merge()
相反,您必须使用 array_merge()
$array = array_merge(array('Item 1'), array('Item 2'));
回答by Henry
This works for non-associative arrays:
这适用于非关联数组:
while(($item = array_shift($array2)) !== null && array_push($array1, $item));
while(($item = array_shift($array2)) !== null && array_push($array1, $item));
回答by Josh K
Try saying
试着说
$array[] = array('Item 2');
Although it looks like you're trying to add an array into an array, thus $array[][]but that's not what your title suggests.
虽然看起来您正在尝试将数组添加到数组中,$array[][]但这并不是您的标题所暗示的。
回答by randomWalk
you may use operator . $array3 = $array1.$array2;
您可以使用 operator 。$array3 = $array1.$array2;

