Using + To Merge Arrays With Php

Why does this not work? Skipping the shorthand & using $array = $array + array("Item 2") does not work either. Does it have something to vày with the keys?


Introducing: Trending sort You can now choose lớn sort by Trending, which boosts votes that have happened recently, helping to surface more up-to-date answers.

Bạn đang xem: Using + to merge arrays with php

Trending is based off of the highest score sort and falls back khổng lồ it if no posts are trending.

Try it Dismiss
Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
Both will have a key of 0, và that method of combining the arrays will collapse duplicates. Try using array_merge() instead.

$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 lớn clarify

Improve this answer
answered Apr 16, 2010 at 2:25

16.1k44 gold badges2424 silver badges1818 bronze badges
add a phản hồi |
Use array_merge()See the documentation here:

Merges the elements of one or more arrays together so that the values of one are appended to lớn the end of the previous one. It returns the resulting array.

mô tả
Improve this answer
answered Apr 16, 2010 at 2:25

Christopher AltmanChristopher Altman
4,80022 gold badges3131 silver badges4949 bronze badges
địa chỉ a phản hồi |
IMO some of the previous answers are incorrect!(It"s possible to sort the answers khổng lồ start from oldest khổng lồ newest).

array_merge() actually merges the arrays, meaning, if the arrays have a common cống phẩm one of the copies will be omitted. Same goes for + (union).

I didn"t find a "work-around" for this issue, but to vị it manually...

Here it goes:

tóm tắt
Improve this answer
edited Sep 24, 2020 at 11:06
answered Jan 3, 2012 at 2:40


Nir AlfasiNir Alfasi
51.9k1111 gold badges8181 silver badges121121 bronze badges
showroom a phản hồi |
use the splat ( or spread ) operator:

$animals = <"dog", "cat", "snake", "pig", "chicken">; $fruits = <"apple", "banana", "water melon">; $things = <...$animals, ...$fruits>;source:

chia sẻ
Improve this answer
edited May 10, 2021 at 17:05

5,67833 gold badges4040 silver badges5858 bronze badges
answered May 5, 2021 at 19:25
Sergio A. KesslerSergio A. Kessler
15911 silver badge66 bronze badges
showroom a bình luận |
+ is called the Union operator, which differs from a Concatenation operator (PHP doesn"t have one for arrays). The description clearly says:

The + operator appends elements of remaining keys from the right handed array lớn the left handed, whereas duplicated keys are NOT overwritten.

Xem thêm: Hiệp Định Aanzfta Là Gì - Tổng Quan Fta Giữa Việt Nam

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.

To concatenate, use array_merge.

nội dung
Improve this answer
edited Apr 16, 2010 at 2:37
answered Apr 16, 2010 at 2:28
493k8181 gold badges709709 silver badges855855 bronze badges
địa chỉ cửa hàng a bình luận |
Try 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 khổng lồ either, so they both have key of 0, & the + does not re-index, so its trying lớn over write it.

nội dung
Improve this answer
answered Apr 16, 2010 at 2:26
4,19211 gold badge2929 silver badges5353 bronze badges
địa chỉ a bình luận |
It is indeed a key conflict. When concatenating arrays, duplicate keys are not overwritten.

Instead you must use array_merge()

$array = array_merge(array("Item 1"), array("Item 2"));
chia sẻ
Improve this answer
edited Nov 13, 2011 at 16:49
81.5k2727 gold badges202202 silver badges228228 bronze badges
answered Apr 16, 2010 at 2:28
Rupert Madden-AbbottRupert Madden-Abbott
12.6k1414 gold badges5757 silver badges7171 bronze badges
địa chỉ cửa hàng a phản hồi |
$array = array("Item 1");array_push($array,"Item 2");or

$array<> = "Item 2";
tóm tắt
Improve this answer
answered Apr 16, 2010 at 2:27
Brant MessengerBrant Messenger
1,4411111 silver badges1919 bronze badges
showroom a comment |
This works for non-associative arrays:

while(($item = array_shift($array2)) !== null && array_push($array1, $item));

giới thiệu
Improve this answer
answered Apr 19, 2018 at 5:38
7,43122 gold badges3535 silver badges3737 bronze badges
địa chỉ cửa hàng a phản hồi |
Try saying

$array<> = array("Item 2"); Although it looks lượt thích you"re trying to địa chỉ cửa hàng an array into an array, thus $array<><> but that"s not what your title suggests.

tóm tắt
Improve this answer
answered Apr 16, 2010 at 2:25
Josh KJosh K
27.5k2020 gold badges8383 silver badges132132 bronze badges
địa chỉ cửa hàng a phản hồi |
you may use operator . $array3 = $array1.$array2;

nội dung
Improve this answer
answered Jun 2, 2015 at 22:22
địa chỉ a comment |

Your Answer

Thanks for contributing an answer lớn Stack Overflow!

Please be sure khổng lồ answer the question. Provide details & share your research!

But avoid

Asking for help, clarification, or responding lớn other answers.Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Facebook
Sign up using email and Password

Post as a guest

e-mail Required, but never shown

Post as a guest

thư điện tử

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree khổng lồ our terms of service, privacy policy & cookie policy

Not the answer you're looking for? Browse other questions tagged php arrays concatenation or ask your own question.

The Overflow Blog
Featured on Meta
array merge for numeric values
How to combine different arrays inside an array into one single dimensional array in php?
PHP : How combine array lượt thích layer
+ operator for array in PHP?
PHP - Duplicate values in an array
Joining arrays khổng lồ go in khổng lồ a multidimensional array
How to địa chỉ cửa hàng two vectors of the same kích thước in php
How to 1 output from 2 output
How can I concatenate two arrays in Java?
Deleting an element from an array in PHP
Preferred method lớn store PHP arrays (json_encode vs serialize)
How vày I concatenate two lists in Python?
Reference — What does this symbol mean in PHP?
Pretty-Printing JSON with PHP
How khổng lồ compare arrays in JavaScript?
How does PHP 'foreach' actually work?
Reference - What does this error mean in PHP?
Why shouldn't I use mysql_* functions in PHP?
Hot Network Questions more hot questions

Question feed
Subscribe to lớn RSS
Question feed lớn subscribe to lớn this RSS feed, copy và paste this URL into your RSS reader.

Stack Overflow
Stack Exchange Network
Site kiến thiết / biểu tượng logo © 2022 Stack Exchange Inc; user contributions licensed under cc by-sa. Rev2022.7.4.42509

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.