PHP append one array to another (not array_push or +) - php

How to append one array to another without comparing their keys?
$a = array( 'a', 'b' );
$b = array( 'c', 'd' );
At the end it should be: Array( [0]=>a [1]=>b [2]=>c [3]=>d )
If I use something like [] or array_push, it will cause one of these results:
Array( [0]=>a [1]=>b [2]=>Array( [0]=>c [1]=>d ) )
//or
Array( [0]=>c [1]=>d )
It just should be something, doing this, but in a more elegant way:
foreach ( $b AS $var )
$a[] = $var;

array_merge is the elegant way:
$a = array('a', 'b');
$b = array('c', 'd');
$merge = array_merge($a, $b);
// $merge is now equals to array('a','b','c','d');
Doing something like:
$merge = $a + $b;
// $merge now equals array('a','b')
Will not work, because the + operator does not actually merge them. If they $a has the same keys as $b, it won't do anything.

Another way to do this in PHP 5.6+ would be to use the ... token
$a = array('a', 'b');
$b = array('c', 'd');
array_push($a, ...$b);
// $a is now equals to array('a','b','c','d');
This will also work with any Traversable
$a = array('a', 'b');
$b = new ArrayIterator(array('c', 'd'));
array_push($a, ...$b);
// $a is now equals to array('a','b','c','d');
A warning though:
in PHP versions before 7.3 this will cause a fatal error if $b is an empty array or not traversable e.g. not an array
in PHP 7.3 a warning will be raised if $b is not traversable

Why not use
$appended = array_merge($a,$b);
Why don't you want to use this, the correct, built-in method.

It's a pretty old post, but I want to add something about appending one array to another:
If
one or both arrays have associative keys
the keys of both arrays don't matter
you can use array functions like this:
array_merge(array_values($array), array_values($appendArray));
array_merge doesn't merge numeric keys so it appends all values of $appendArray. While using native php functions instead of a foreach-loop, it should be faster on arrays with a lot of elements.
Addition 2019-12-13:
Since PHP 7.4, there is the possibility to append or prepend arrays the Array Spread Operator way:
$a = [3, 4];
$b = [1, 2, ...$a];
As before, keys can be an issue with this new feature:
$a = ['a' => 3, 'b' => 4];
$b = ['c' => 1, 'a' => 2, ...$a];
"Fatal error: Uncaught Error: Cannot unpack array with string keys"
$a = [3 => 3, 4 => 4];
$b = [1 => 1, 4 => 2, ...$a];
array(4) {
[1]=>
int(1)
[4]=>
int(2)
[5]=>
int(3)
[6]=>
int(4)
}
$a = [1 => 1, 2 => 2];
$b = [...$a, 3 => 3, 1 => 4];
array(3) {
[0]=>
int(1)
[1]=>
int(4)
[3]=>
int(3)
}

<?php
// Example 1 [Merging associative arrays. When two or more arrays have same key
// then the last array key value overrides the others one]
$array1 = array("a" => "JAVA", "b" => "ASP");
$array2 = array("c" => "C", "b" => "PHP");
echo " <br> Example 1 Output: <br>";
print_r(array_merge($array1,$array2));
// Example 2 [When you want to merge arrays having integer keys and
//want to reset integer keys to start from 0 then use array_merge() function]
$array3 =array(5 => "CSS",6 => "CSS3");
$array4 =array(8 => "JAVASCRIPT",9 => "HTML");
echo " <br> Example 2 Output: <br>";
print_r(array_merge($array3,$array4));
// Example 3 [When you want to merge arrays having integer keys and
// want to retain integer keys as it is then use PLUS (+) operator to merge arrays]
$array5 =array(5 => "CSS",6 => "CSS3");
$array6 =array(8 => "JAVASCRIPT",9 => "HTML");
echo " <br> Example 3 Output: <br>";
print_r($array5+$array6);
// Example 4 [When single array pass to array_merge having integer keys
// then the array return by array_merge have integer keys starting from 0]
$array7 =array(3 => "CSS",4 => "CSS3");
echo " <br> Example 4 Output: <br>";
print_r(array_merge($array7));
?>
Output:
Example 1 Output:
Array
(
[a] => JAVA
[b] => PHP
[c] => C
)
Example 2 Output:
Array
(
[0] => CSS
[1] => CSS3
[2] => JAVASCRIPT
[3] => HTML
)
Example 3 Output:
Array
(
[5] => CSS
[6] => CSS3
[8] => JAVASCRIPT
[9] => HTML
)
Example 4 Output:
Array
(
[0] => CSS
[1] => CSS3
)
Reference Source Code

Following on from answer's by bstoney and Snark I did some tests on the various methods:
// Test 1 (array_merge)
$array1 = $array2 = array_fill(0, 50000, 'aa');
$start = microtime(true);
$array1 = array_merge($array1, $array2);
printf("Test 1: %.06f\n", microtime(true) - $start);
// Test2 (foreach)
$array1 = $array2 = array_fill(0, 50000, 'aa');
$start = microtime(true);
foreach ($array2 as $v) {
$array1[] = $v;
}
printf("Test 2: %.06f\n", microtime(true) - $start);
// Test 3 (... token)
// PHP 5.6+ and produces error if $array2 is empty
$array1 = $array2 = array_fill(0, 50000, 'aa');
$start = microtime(true);
array_push($array1, ...$array2);
printf("Test 3: %.06f\n", microtime(true) - $start);
Which produces:
Test 1: 0.002717
Test 2: 0.006922
Test 3: 0.004744
ORIGINAL: I believe as of PHP 7, method 3 is a significantly better alternative due to the way foreach loops now act, which is to make a copy of the array being iterated over.
Whilst method 3 isn't strictly an answer to the criteria of 'not array_push' in the question, it is one line and the most high performance in all respects, I think the question was asked before the ... syntax was an option.
UPDATE 25/03/2020:
I've updated the test which was flawed as the variables weren't reset. Interestingly (or confusingly) the results now show as test 1 being the fastest, where it was the slowest, having gone from 0.008392 to 0.002717! This can only be down to PHP updates, as this wouldn't have been affected by the testing flaw.
So, the saga continues, I will start using array_merge from now on!

For big array, is better to concatenate without array_merge, for avoid a memory copy.
$array1 = array_fill(0,50000,'aa');
$array2 = array_fill(0,100,'bb');
// Test 1 (array_merge)
$start = microtime(true);
$r1 = array_merge($array1, $array2);
echo sprintf("Test 1: %.06f\n", microtime(true) - $start);
// Test2 (avoid copy)
$start = microtime(true);
foreach ($array2 as $v) {
$array1[] = $v;
}
echo sprintf("Test 2: %.06f\n", microtime(true) - $start);
// Test 1: 0.004963
// Test 2: 0.000038

Since PHP 7.4 you can use the ... operator. This is also known as the splat operator in other languages, including Ruby.
$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
var_dump($fruits);
Output
array(5) {
[0]=>
string(6) "banana"
[1]=>
string(6) "orange"
[2]=>
string(5) "apple"
[3]=>
string(4) "pear"
[4]=>
string(10) "watermelon"
}
Splat operator should have better performance than array_merge. That’s not only because the splat operator is a language structure while array_merge is a function, but also because compile time optimization can be performant for constant arrays.
Moreover, we can use the splat operator syntax everywhere in the array, as normal elements can be added before or after the splat operator.
$arr1 = [1, 2, 3];
$arr2 = [4, 5, 6];
$arr3 = [...$arr1, ...$arr2];
$arr4 = [...$arr1, ...$arr3, 7, 8, 9];

Before PHP7 you can use:
array_splice($a, count($a), 0, $b);
array_splice() operates with reference to array (1st argument) and puts array (4th argument) values in place of list of values started from 2nd argument and number of 3rd argument. When we set 2nd argument as end of source array and 3rd as zero we append 4th argument values to 1st argument

if you want to merge empty array with existing new value. You must initialize it first.
$products = array();
//just example
for($brand_id=1;$brand_id<=3;$brand_id++){
array_merge($products,getByBrand($brand_id));
}
// it will create empty array
print_r($a);
//check if array of products is empty
for($brand_id=1;$brand_id<=3;$brand_id++){
if(empty($products)){
$products = getByBrand($brand_id);
}else{
array_merge($products,getByBrand($brand_id));
}
}
// it will create array of products
Hope its help.

foreach loop is faster than array_merge to append values to an existing array, so choose the loop instead if you want to add an array to the end of another.
// Create an array of arrays
$chars = [];
for ($i = 0; $i < 15000; $i++) {
$chars[] = array_fill(0, 10, 'a');
}
// test array_merge
$new = [];
$start = microtime(TRUE);
foreach ($chars as $splitArray) {
$new = array_merge($new, $splitArray);
}
echo microtime(true) - $start; // => 14.61776 sec
// test foreach
$new = [];
$start = microtime(TRUE);
foreach ($chars as $splitArray) {
foreach ($splitArray as $value) {
$new[] = $value;
}
}
echo microtime(true) - $start; // => 0.00900101 sec
// ==> 1600 times faster

Related

How can I merge two associative arrays and preserve the global order of the entries?

I have two arrays like this:
$arr1 = ['a' => '1','b' => 2];
$arr2 = ['h' => 'c','j' => '3'];
And I want to merge them to this result:
$newArr = ['a' => '1','h'=>'c','b'=>2,'j' => '3'];
That means I want to merge them so that the global order of the entries is the same as in the source arrays. In other words, zip and flatten.
array_merge does not do this. Is there any solution?
Note that this solution will only work if the two arrays have the same length:
$arr1 = [ 'a' => '1', 'b' => 2 ];
$arr2 = [ 'h' => 'c', 'j' => '3' ];
$count = count($arr1);
$keys1 = array_keys($arr1);
$keys2 = array_keys($arr2);
$result = [];
for ($i = 0; $i < $count; $i++) {
$key1 = $keys1[$i];
$result[$key1] = $arr1[$key1];
$key2 = $keys2[$i];
$result[$key2] = $arr2[$key2];
}
print_r($result);
Output:
Array
(
[a] => 1
[h] => c
[b] => 2
[j] => 3
)
Edited based on mickmackusa's comment below.
First is a solution that will consume the input arrays in a loop while building the new structure. You can always cache separate copies of the input if you need them elsewhere.
All solutions below will work even if the two arrays have different lengths -- any remaining elements will be appended to the end of the result array after the loop.
Code: (Demo)
$result = [];
while ($arr1 && $arr2) {
$result += array_splice($arr1, 0, 1)
+ array_splice($arr2, 0, 1);
}
$result += $arr1 + $arr2;
var_export($result);
Another way without consuming the input arrays is to build lookup arrays:
Code: (Demo)
$max = max(count($arr1), count($arr2));
$keys1 = array_keys($arr1);
$keys2 = array_keys($arr2);
$result = [];
for ($x = 0; $x < $max; ++$x) {
if (isset($keys1[$x])) {
$result[$keys1[$x]] = $arr1[$keys1[$x]];
}
if (isset($keys2[$x])) {
$result[$keys2[$x]] = $arr2[$keys2[$x]];
}
}
var_export($result);
Or you could use array_slice() to isolate one element at a time from each array without damaging the input arrays, nor generating warnings.
Code: (Demo)
$result = [];
for ($i = 0, $count = count($arr1); $i < $count; ++$i) {
$result += array_slice($arr1, $i, 1)
+ array_slice($arr2, $i, 1);
}
$result += $arr1 + $arr2;
You can use array_merge() or array_merge_recursive().
Merges the elements of one or more arrays such that the values of one array are appended to the end of the previous one. The result of the function is a new array.
https://www.php.net/manual/ru/function.array-merge.php
The array_merge_recursive() function merges the elements of two or more arrays in such a way that the values ​​of one array are appended to the end of the other. Returns the resulting array.
If the input arrays have the same string keys, then the values ​​of those keys are merged into an array, and this is done recursively, so that if one of the values ​​is an array, then the function merges it with the corresponding value in the other array. However, if the arrays have the same numeric keys, each successive value will not replace the original value, but will be added to the end of the array.
https://www.php.net/manual/ru/function.array-merge-recursive.php
you can use array_merge function,which uses to merge two or more arrays into single array.
$arr1 = ['a' => '1','b' => 2];
$arr2 = ['h' => 'c','j' => '3'];
$result=array_merge($arr1,$arr2);
var_dump($result);
//output:- array(4) { ["a"]=> string(1) "1" ["b"]=> int(2) ["h"]=> string(1) "c" ["j"]=> string(1) "3" }

Implode columnar values between two arrays into a flat array of concatenated strings

I have two arrays, $array_A and $array_B. I'd like to append the first value from $array_B to the end of the first value of $array_A and repeat this approach for all elements in both arrays.
$array_A = ['foo', 'bar', 'quuz'];
$array_B = ['baz', 'qux', 'corge'];
Expected output after squishing:
['foobaz', 'barqux', 'quuzcorge']
array_merge($array_A, $array_B) simply appends array B onto array A, and array_combine($array_A, $array_B) sets array A to be the key for array B, neither of which I want. array_map seems pretty close to what I want, but seems to always add a space between the two, which I don't want.
It would be ideal for the lengths of each array it to be irrelevant (e.g. array A has five entries, array B has seven entries, extra entries are ignored/trimmed) but not required.
// updated version
$a = ['a', 'b', 'c'];
$b = ['d', 'e', 'f', 'g'];
print_r(array_map('implode', array_map(null, $a, $b)));
Probably the fastest code, but more verbose than other options.
//updated version
$array_A = ['foo', 'bar', 'quuz'];
$array_B = ['baz', 'qux', 'corge'];
for ($i = 0, $c = count($array_A); $i<$c; $i++) {
$result[$i] = $array_A[$i].$array_B[$i];
}
var_dump($result);
There isn't an array function in PHP that does exactly that. However, you can write one yourself, like this one:
function array_zip($a1, $a2) {
$out = [];
for($i = 0; $i < min(sizeof($a1), sizeof($a2)); $i++) {
array_push($out, $a1[$i] . $a2[$i]);
}
return $out;
}
So given these arrays and running it:
$a = ["foo", "bar"];
$b = ["baz", "qux"];
print_r(array_zip($a, $b));
You would get:
Array
(
[0] => foobaz
[1] => barqux
)
Try this:
$A = ['foo', 'bar'];
$B = ['baz', 'qux'];
function arraySquish($array)
{
$new = [''];
foreach($array as $val) {
$new[0] .= $val;
}
return $new;
}
$A = arraySquish($A);
$B = arraySquish($B);
echo '<pre>';
print_r($A);
print_r($B);
echo '</pre>';
PHP Fiddle here.
An explicit array_map:
<?php
$colours = ['red', 'white', 'blue'];
$items = ['robin', 'cloud', 'mountain'];
$squished =
array_map(
function($colour, $item) {
return $colour.$item;
},
$colours,
$items
);
var_export($squished);
Output:
array (
0 => 'redrobin',
1 => 'whitecloud',
2 => 'bluemountain',
)
If you want to only go as far as the smallest array, you could either return null if either entries are null, and then filter your result.
Or truncate the arrays to the same length:
$b = array_intersect_key($b, $a);
$a = array_intersect_key($a, $b);
Regardless of if both arrays have the same length or if one is longer than the other or in what order the arrays occur in, the following technique will "squish" the values into a flat array of strings.
array_map() only needs to be called once and implode()'s default "glue" string is an empty string -- so it can be omitted.
Code: (Demo)
$a = ['a', 'b', 'c'];
$b = ['d', 'e', 'f', 'g'];
var_export(array_map(fn() => implode(func_get_args()), $a, $b));
Or consolidate the column data with spread operator: (Demo)
var_export(array_map(fn(...$column) => implode($column), $a, $b));
Output:
array (
0 => 'ad',
1 => 'be',
2 => 'cf',
3 => 'g',
)

Set Theory Union of arrays in PHP

There are 3 operations with sets in mathematics: intersection, difference and union (unification). In PHP we can do this operations with arrays:
intersection: array_intersect
difference: array_diff
What function is for union?
No duplicates can be in the result array (like array_intersect and array_diff).
If indexes are numeric then array_merge will not overwrite the original value, but will be appended (PHP docs).
Try array_merge:
array_unique(array_merge($array1, $array2));
PHP Manual
Adrien's answer won't necessary produce a sequentially numbered array from two sequentially numbered arrays - here are some options that will:
array_values(array_unique(array_merge($array1, $array2)));
(Adrien's answer with renumbering the keys afterward)
array_keys(array_flip($array1)+array_flip($array2))
(Put the values in the keys, and use the array union operator)
array_merge($array1, array_diff($array2, $array1))
(Remove the shared values from the second array before merging)
Benchmark results (for merging two arrays of length 1000 a thousand times on my system):
Unique (Adrien's version): 2.862163066864 seconds
Values_Unique: 3.12 seconds
Keys_Flip: 2.34 seconds
Merge_Diff: 2.64 seconds
Same test, but with the two arrays being very similar (at least 80% duplicate):
Unique (Adrien's version): 2.92 seconds
Values_Unique: 3.15 seconds
Keys_Flip: 1.84 seconds
Merge_Diff: 2.36 seconds
It seems using the array union operator to do the actual union is the fastest method. Note however that array_flip is only safe if the array's values are all strings or all integers; if you have to produce the union of an array of objects, I recommend the version with array_merge and array_diff.
array_unique( array_merge( ... ) )
use "+" operator to do so. See the link Array Operators
Use array_unique and array_merge together.
From the code in the PHP: Array Operators documentation:
<?php
$a = array("a" => "apple", "b" => "banana");
$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");
$c = $a + $b; // Union of $a and $b
echo "Union of \$a and \$b: \n";
var_dump($c);
$c = $b + $a; // Union of $b and $a
echo "Union of \$b and \$a: \n";
var_dump($c);
?>
When executed, this script will print the following:
Union of $a and $b:
array(3) {
["a"]=>
string(5) "apple"
["b"]=>
string(6) "banana"
["c"]=>
string(6) "cherry"
}
Union of $b and $a:
array(3) {
["a"]=>
string(4) "pear"
["b"]=>
string(10) "strawberry"
["c"]=>
string(6) "cherry"
}
The OP did not specify whether the union is by value or by key and PHP has array_merge for merging values and + for merging the keys. The results depends on whether the array is indexed or keyed and which array comes first.
$a = ['a', 'b'];
$b = ['b', 'c'];
$c = ['a' => 'A', 'b' => 'B'];
‌‌$d = ['a' => 'AA', 'c' => 'C'];
Indexed array
See array_merge
By value using array_merge
‌‌array_merge($a, $b); // [‌0 => 'a', 1 => 'b', 2 => 'b', 3 => 'c']
‌‌array_merge($b, $a); // ‌[0 => 'b', 1 => 'c', 2 => 'a', 3 => 'b']
merge by key using + operator
See + operator
‌‌$a + $b; ‌// [0 => 'a', 1 => 'b']
$b + $a; // [0 => 'b', 1 => 'c']
Keyed array
By value using array_merge
‌array_merge($c, $d); // ‌['a' => 'AA', 'b' => 'B', 'c' => 'C']
array_merge($d, $c); // ['a' => 'A', 'c' => 'C', 'b' => 'B']
merge by key using + operator
$c + $d; // [‌'a' => 'A', 'b' => 'B', 'c' => 'C']
‌‌$d + $c; // ‌['a' => 'AA', 'c' => 'C', 'b' => 'B']
$result = array_merge_recursive($first, $second);
can be useful when you have arrays with arrays inside.
The + operator:
$x[0] = 4;
$x[1] = 1;
$y[0] = 9;
$y[2] = 5;
$u = $y + $x;
// Results in:
$u[0] === 9;
$u[1] === 1;
$u[2] === 5;
Note that $x + $y != $y + $x
Just use $array1 + $array2
It will result union of both array.
Try this:
function array_union($array1, $array2){
return array_unique(array_merge($array1, $array2));
}

How to merge two arrays by taking over only values from the second array that has the same keys as the first one?

I'd like to merge two arrays with each other:
$filtered = array(1 => 'a', 3 => 'c');
$changed = array(2 => 'b*', 3 => 'c*');
Whereas the merge should include all elements of $filtered and all those elements of $changed that have a corresponding key in $filtered:
$merged = array(1 => 'a', 3 => 'c*');
array_merge($filtered, $changed) would add the additional keys of $changed into $filtered as well. So it does not really fit.
I know that I can use $keys = array_intersect_key($filtered, $changed) to get the keys that exist in both arrays which is already half of the work.
However I'm wondering if there is any (native) function that can reduce the $changed array into an array with the $keys specified by array_intersect_key? I know I can use array_filter with a callback function and check against $keys therein, but there is probably some other purely native function to extract only those elements from an array of which the keys can be specified?
I'm asking because the native functions are often much faster than array_filter with a callback.
This should do it, if I'm understanding your logic correctly:
array_intersect_key($changed, $filtered) + $filtered
Implementation:
$filtered = array(1 => 'a', 3 => 'c');
$changed = array(2 => 'b*', 3 => 'c*');
$expected = array(1 => 'a', 3 => 'c*');
$actual = array_key_merge_deceze($filtered, $changed);
var_dump($expected, $actual);
function array_key_merge_deceze($filtered, $changed) {
$merged = array_intersect_key($changed, $filtered) + $filtered;
ksort($merged);
return $merged;
}
Output:
Expected:
array(2) {
[1]=>
string(1) "a"
[3]=>
string(2) "c*"
}
Actual:
array(2) {
[1]=>
string(1) "a"
[3]=>
string(2) "c*"
}
if you want the second array ($b) to be the pattern that indicates that if there is only the key there, then you could also try this
$new_array = array_intersect_key( $filtered, $changed ) + $changed;
If your keys are non-numeric (which yours are not, so this is not a solution to your exact question), then you can use this technique:
$filtered = array('a' => 'a', 'c' => 'c');
$changed = array('b' => 'b*', 'c' => 'c*');
$merged = array_slice(array_merge($filtered, $changed), 0, count($filtered));
Result:
Array
(
[a] => a
[c] => c*
)
This works because for non-numeric keys, array_merge overwrites values for existing keys, and appends the keys in $changed to the end of the new array. So we can simply discard any keys from the end of the merged array more than the count of the original array.
Since this applies to the same question but with different key types I thought I'd provide it.
If you use this with numeric keys then the result is simply the original array ($filtered in this case) with re-indexed keys (IE as if you used array_values).

Can't concatenate 2 arrays in PHP

I've recently learned how to join 2 arrays using the + operator in PHP.
But consider this code...
$array = array('Item 1');
$array += array('Item 2');
var_dump($array);
Output is
array(1) { [0]=> string(6) "Item
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?
Both will have a key of 0, and 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 to clarify
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 the end of the previous one. It returns the resulting array.
IMO some of the previous answers are incorrect!
(It's possible to sort the answers to start from oldest to newest).
array_merge() actually merges the arrays, meaning, if the arrays have a common item one of the copies will be omitted. Same goes for + (union).
I didn't find a "work-around" for this issue, but to 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);
?>
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/
+ 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 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.
To concatenate, use array_merge.
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 to either, so they both have key of 0, and the + does not re-index, so its trying to over write it.
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'));
$array = array('Item 1');
array_push($array,'Item 2');
or
$array[] = 'Item 2';
This works for non-associative arrays:
while(($item = array_shift($array2)) !== null && array_push($array1, $item));
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.
you may use operator .
$array3 = $array1.$array2;

Categories