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


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

awgyawgy
16.1k44 gold badges2424 silver badges1818 bronze badges
3
add a phản hồi |
11
Use array_merge()See the documentation here: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 lớn the end of the previous one. It returns the resulting array.


mô tả
Improve this answer
Follow
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 |
9
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
Follow
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
4
showroom a phản hồi |
8
use the splat ( or spread ) operator:

$animals = <"dog", "cat", "snake", "pig", "chicken">; $fruits = <"apple", "banana", "water melon">; $things = <...$animals, ...$fruits>;source: https://www.kindacode.com/article/merging-arrays-in-php-7/


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

dansch
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 |
7
+ 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
Follow
edited Apr 16, 2010 at 2:37
answered Apr 16, 2010 at 2:28
deceze♦deceze
493k8181 gold badges709709 silver badges855855 bronze badges
0
địa chỉ cửa hàng a bình luận |
3
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
Follow
answered Apr 16, 2010 at 2:26
RabbottRabbott
4,19211 gold badge2929 silver badges5353 bronze badges
địa chỉ a bình luận |
3
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
Follow
edited Nov 13, 2011 at 16:49
NullUserException
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 |
2
$array = array("Item 1");array_push($array,"Item 2");or

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

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


giới thiệu
Improve this answer
Follow
answered Apr 19, 2018 at 5:38
HenryHenry
7,43122 gold badges3535 silver badges3737 bronze badges
địa chỉ cửa hàng a phản hồi |
-3
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
Follow
answered Apr 16, 2010 at 2:25
Josh KJosh K
27.5k2020 gold badges8383 silver badges132132 bronze badges
2
địa chỉ cửa hàng a phản hồi |
-5
you may use operator . $array3 = $array1.$array2;


nội dung
Improve this answer
Follow
answered Jun 2, 2015 at 22:22
randomWalkrandomWalk
1
1
đị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
Submit

Post as a guest


Name
e-mail Required, but never shown


Post as a guest


Name
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
Linked
0
array merge for numeric values
0
How to combine different arrays inside an array into one single dimensional array in php?
-1
PHP : How combine array lượt thích layer
214
+ operator for array in PHP?
11
PHP - Duplicate values in an array
1
Joining arrays khổng lồ go in khổng lồ a multidimensional array
1
How to địa chỉ cửa hàng two vectors of the same kích thước in php
-2
How to 1 output from 2 output
Related
1526
How can I concatenate two arrays in Java?
2904
Deleting an element from an array in PHP
644
Preferred method lớn store PHP arrays (json_encode vs serialize)
3246
How vày I concatenate two lists in Python?
4934
Reference — What does this symbol mean in PHP?
725
Pretty-Printing JSON with PHP
1377
How khổng lồ compare arrays in JavaScript?
2208
How does PHP 'foreach' actually work?
1241
Reference - What does this error mean in PHP?
2659
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.


lang-php
Stack Overflow
Products
Company
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.