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
4
votes
3 answers

Group subarrays based on column value

I have an array: [['a', 1], ['b', 1], ['c',2], ['d',2]] How can I group the subarrays based on the second column value like this: [[['a', 1], ['b', 1]], [['c',2], ['d',2]]] I have an idea to solve this with a foreach, but it may have a way with…
Ticksy
  • 561
  • 2
  • 10
  • 19
3
votes
3 answers

jquery distinct array

I have array textbox something like below
foekall
  • 79
  • 2
  • 7
3
votes
4 answers

How to create a new array of objects from two arrays based on object values?

I'm struggling with merging two arrays of objects (fetched from blockchain data) into a new array based on object values. The goal is to get the latest interaction with a user. A simplified but close representation of the data structure this problem…
tenxsoydev
  • 370
  • 2
  • 10
3
votes
4 answers

Merging 4 sorted Arrays into one

I have this method to merge 2 sorted arrays into one sorted array: public void merge(T[] a, int l1, int r1, T[] b, int l2, int r2, T[] c, int l3) { while (l1 < r1 && l2 < r2) { if (a[l1].compareTo(b[l2]) < 0) { …
Kicksy
  • 83
  • 1
  • 7
3
votes
3 answers

Use recursion to accumulate rows without depending on class property variable

Having this array : [ "id" => 5, "name" => "Item 5", "all_parents" => [ "id" => 4, "name" => "Item 4", "all_parents" => [ "id" => 3, "name" => "Item 3", "all_parents" => [ …
3
votes
2 answers

Flatten Array: Keep index, value equal to position in array

I've been having a little trouble trying to flatten arrays in a specific way. Here is a print_r view of the array I want to flatten: Array ( [1] => Array ( [8] => 1 [9] => 2 …
nderjung
  • 1,607
  • 4
  • 17
  • 38
3
votes
2 answers

Merge associative row data in a 2d array based on a column value

I need to merge the row data of my 2d array where rows share the same messageID value. $myarray = [ [ 'messageId' => '5ACE9D8841', 'sender' => 'john@doe.com' ], [ 'messageId' => '7EE67D8170', 'sender' =>…
3
votes
1 answer

How to avoid array_merge in loops when dealing with array of objects

There are a bunch of discussions around 'array_merge(...)' is used in a loop and is a resources greedy construction. For simple arrays there is an easy solution using the spread operator. E.g. $arraysToMerge = [ [1, 2], [2, 3], [5,8]…
JakobAttk
  • 113
  • 3
  • 16
3
votes
2 answers

BigQuery: Concatenate two arrays and keep distinct values within MERGE statement

I am working on a MERGE process and update an array field with new data but only if the value isn't already found in the array. target table +-----+----------+ | id | arr_col | +-----+----------+ | a | [1,2,3] | | b | [0] …
3
votes
1 answer

Copy some properties from 1 list to another based on property (distinct properties merge)

Firstly: I can't figure a proper title for this post. Secondly: I have 2 lists and I am trying to merge/update some of the list properties base on one key. List result = new ArrayList(); List intermediaryData = new…
SanRyu
  • 210
  • 1
  • 2
  • 13
3
votes
4 answers

PHP::How merge 2 arrays when array 1 values will be in even places and array 2 will be in odd places?

How can I merge two arrays when array 1 values will be in even places and array 2 will be in odd places? Example: $arr1=array(11, 34,30); $arr2=array(12, 666); $output=array(11, 12, 34, 666,30);
Ben
  • 25,389
  • 34
  • 109
  • 165
3
votes
2 answers

Why is array_merge_recursive not recursive?

I recently found a bug in my application caused by unexpected behaviour of array_merge_recursive. Let's take a look at this simple example: $array1 = [ 1 => [ 1 => 100, 2 => 200, ], 2 => [ 3 => 1000, ], 3…
Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
3
votes
2 answers

how to merge two arrays according to their respective key?

how to combine two arrays within their respective keys i tryied array_merge but it did not work as I want. I also tried with array_merge_recursive... the same... these are my two arrays: array 1 : array(2) { [264]=> array(6) { [0]=> …
Meta Code
  • 557
  • 1
  • 7
  • 15
3
votes
5 answers

Map/Merge data from a flat array into the rows of a 2d array

I want to add the values of array b to array a: $a = [[1, 2],[4, 5],[7, 8]]; $b = [3, 6, 9]; Result should be: $result = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]; I am trying this (and lots of other stuff) but don't get it. foreach ($a as $el) { $i =…
3
votes
3 answers

How to merge two arrays and sum the values of duplicate keys?

I would like to merge array by conditions. If array keys match then add the values, if not then retain the value. Here are my arrays: Array1 ( [1] => 199 [3] => 1306 [5] => 199 ) Array2 ( [3] => 199 [4] => 199 ) My desired…
SAHAR
  • 49
  • 1
  • 8