Questions tagged [array-merge]

Array-merge means elements of one or more arrays will be merged together into a single array.

Array-merge means merging the elements of one or more arrays together into a single array so that the values of one are appended to the end of the previous one.

array array_merge ( array $array1 [, array $... ] )

Example

$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);

Result

Array
(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)

Reference

819 questions
11
votes
4 answers

What does $array2 += $array1 do?

I've seen the following often lately and I'm wondering what it does? I can't seem to find it in the PHP manual. $arr1 = array('key' => 'value1'); $arr2 = array('key' => 'value2'); $arr1 += $arr2; Is it similar to an array_merge? I know what the…
Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
10
votes
4 answers

Why would you merge $_GET and $_POST in PHP?

I just saw this code while studying the wordpress source code (PHP), You can see they mergre/turn all get and post values into 1 request array. Now as I know it, $_GET and $_POST are already available by calling $_REQUEST WITHOUT using the…
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
9
votes
2 answers

JQ - Merge two arrays

I'm looking for JQ query that allows me to merge 2 arrays variables (not files) and also take me to overwrite first array with newer value from second array. For example: #!/bin/bash -e firstArrayVariable=' [ { "Key": "A…
Nam Nguyen
  • 5,668
  • 14
  • 56
  • 70
9
votes
6 answers

Merging 3 arrays in PHP

I have 3 arrays as below. $array1 = Array ( [0] => 05/01 [1] => 05/02 ) $array2 =Array ( [0] => ED [1] => P ) $array3 =Array ( [0] => Mon [1] => Tue ) I want to merge these 3 arrays as below $result_array. I have written…
tenten
  • 1,276
  • 2
  • 26
  • 54
9
votes
7 answers

How to merge subarray values and remove duplicates?

$arr[] = array('A','B'); $arr[] = array('C','B'); ... I need to get the merged result of all sub array of $arr . And for duplicated entries,should fetch only one.
user198729
  • 61,774
  • 108
  • 250
  • 348
9
votes
7 answers

Append column values from a 2d array to the rows of another 2d array based on shared column values

Let's say I have following arrays: $first = [ ['id' => 5, 'name' => 'Education'], ['id' => 4, 'name' => 'Computers'], ['id' => 7, 'name' => 'Science'], ['id' => 1, 'name' => 'Sports'], ]; $second = [ ['id' => 1, 'title' =>…
user1292810
  • 1,702
  • 7
  • 19
  • 38
8
votes
4 answers

Merging two arrays by index

Okay, if feel like this should be really simple and accomplished by a function like array_merge() or array_merge_recursive, but I can't quite figure it out. I have two simple arrays structured like the (simplified) example below. I simply want to…
Kerri
  • 1,211
  • 2
  • 15
  • 22
8
votes
3 answers

how to prevent array_merge to renumber numeric keys

i have an array which looks like this: Array ( [0] => Array ( [unit_id] => 1 [unit_name] => Clown Fish) [1] => Array ( [unit_id] => L [unit_name] => Liter ) [2] => Array ( [unit_id] => 2 [unit_name] => Elephant ) [3] => Array (…
dqiu
  • 327
  • 2
  • 5
  • 15
8
votes
3 answers

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…
hakre
  • 193,403
  • 52
  • 435
  • 836
8
votes
1 answer

Does Ruby's Enumerable#zip create arrays internally?

In Ruby - Compare two Enumerators elegantly, it was said The problem with zip is that it creates arrays internally, no matter what Enumerable you pass. There's another problem with length of input params I had a look at the implementation…
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
8
votes
7 answers

Laravel 5 Merge an array

I am trying to merge an array with an input from a form of a random string of numbers In my form I have And in the controller: public…
0w3n86
  • 519
  • 2
  • 4
  • 14
8
votes
5 answers

How to merge arrays in javascript in a different way?

I want to merge arrays a little bit different way. I have 2 or more arrays like: var array1 = ["apple", "banana"]; var array2 = ["apple", "apple", "orange"]; I want the output: var array3 = ["apple", "apple", "banana", "orange"]; So if any given…
u.zzz
  • 83
  • 4
7
votes
9 answers

Merging arrays based on a value of the key

I have two arrays of arrays that have an id key, and I'd like to merge the data together based on that array's key and key value. The data would look something like: $color = [ ['id' => 1, 'color' => 'red'], ['id' => 2, 'color'…
kenshin9
  • 2,215
  • 4
  • 23
  • 38
7
votes
3 answers

PHP array_replace_recursive if scalar, array_merge_recursive if array

I have some default configurations, and some specific configurations which would be configurable. I need to merge the specific configurations into the default configurations. In the case that the specific config option does not exist, the default…
Gravy
  • 12,264
  • 26
  • 124
  • 193
7
votes
4 answers

array_merge vs array_value for resetting array index

I have 1 array that I want to re-index. I have found that both array_values and array_merge functions can do the job (and I don't need 2 arrays for the array_merge function to work). Which is faster for a very large array? I would benchmark this,…
Jamex
  • 732
  • 3
  • 9
  • 17
1
2
3
54 55