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
7
votes
1 answer

array_merge not working as expected

I have an double level array. On the first level there are about 10 indexes. These contain each to a 275 element array which each contains a word. Array ( [0] => Array ( [0] => Suspendisse [1] => Nam. [2] => Amet [3] => amet …
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
6
votes
4 answers

Merge 2 arrays in PHP, keeping only keys from first array

I have these two arrays: $list = [ 'fruit' => [], 'animals' => [], 'objects' => [], ]; $dataArray = [ 'fruit' => 'apple', 'animals' => ['dog', 'cat'], 'asd' => 'bla' ]; I want to merge them so that $list at the end…
Carlo
  • 4,016
  • 7
  • 44
  • 65
6
votes
4 answers

Merge array inside array

I have 2 arrays to merge. The first array is multidimensional, and the second array is a single array: $a = array( array('id'=>'1', 'name'=>'Mike'), array('id'=>'2', 'name'=>'Lina'), ); $b = array('id'=>'3', 'name'=>'Niken'); How to merge…
Stfvns
  • 1,001
  • 5
  • 16
  • 42
6
votes
1 answer

A better php array_merge

I am looking to do this a better way without the need to hardcode the integers for $justPrices[$i]: $pricesResult = array_merge($justPrices[0], $justPrices[1], $justPrices[2], $justPrices[3]); $justPrices is a multidimensional array, containing 4…
freeMagee
  • 434
  • 2
  • 6
  • 18
6
votes
2 answers

PHP array_merge empty values always less prioritar

My goal is to merge 2 different arrays. I have table "a" & "b". Data from table "a" are more prioritar. PROBLEM: if a key from "a" contains an empty value, I would like to take the one from table "b". Here is my code:
Bast
  • 661
  • 2
  • 7
  • 23
6
votes
1 answer

Merging multiple array then sorting by array value count

Please help me, i need to merge multiple arrays then sorting it by array value count. Below is the problem: $array1 = array("abc", "def", "ghi", "jkl", "mno"); $array2 = array("mno", "jkl", "mno", "ghi", "pqr", "stu"); $array3 = array_merge($array1,…
Sofyan Sitorus
  • 77
  • 1
  • 2
  • 6
6
votes
1 answer

using array merge into a foreach loop

I need to merge a new array of alternative information into the loop if they have the alternative information in their profile. Here's my loop: foreach ($doctor->getVars() as $k => $v) { $data['doctor_'. $k] = $v; } foreach…
Head Way
  • 323
  • 2
  • 4
  • 16
6
votes
2 answers

I want to add sub arrays to one single array in php

i have array like this......... Array ( [0] => Array ( [0] => rose [1] => monkey [2] => donkey ) [1] => Array ( [0] => daisy [1] => monkey [2]…
steve
  • 1,365
  • 4
  • 19
  • 33
6
votes
5 answers

Insert elements from one array (one-at-a-time) after every second element of another array (un-even zippering)

What would be an elegant way to merge two arrays, such that the resulting array has two items from the first array followed by a single item from the second array, repeating in this fashion? $array1 = ['A1', 'A2', 'A3', 'A4', 'A5']; // potentially…
wes
  • 734
  • 6
  • 14
6
votes
1 answer

array_merge changes the keys

I got the following array: $arr = array(6 => 'Somedata', 7 => 'Somedata1', 8 => 'Somedata2'); The problem is that, when I use array_merge( (array) "Select the data", $arr);, it does change the array keys into: Array ( [0] => Not specified …
Sapp
  • 624
  • 2
  • 7
  • 13
5
votes
3 answers

Efficient way of merging two numpy masked arrays

I have two numpy masked arrays which I want to merge. I'm using the following code: import numpy as np a = np.zeros((10000, 10000), dtype=np.int16) a[:5000, :5000] = 1 am = np.ma.masked_equal(a, 0) b = np.zeros((10000, 10000),…
prl900
  • 4,029
  • 4
  • 33
  • 40
5
votes
3 answers

Merge multiple arrays to one array in jquery

I am trying to merge my multiple arrays to one array using jquery. I know that we can merge two arrays to one using jquery merge function. And we can loop through those arrays and join them to one too. But I just wanted to know whether there is any…
user2471346
5
votes
5 answers

Union of two arrays containing associative arrays while keeping only unique rows

I Want to Union 2 array $A and $B Example: $A = Array( 0=>array( 'lable' =>"label0", 'id_poste'=>1, 'id_part'=>11 ), 1=>array( 'lable' =>"label1", 'id_poste'=>2, …
tree em
  • 20,379
  • 30
  • 92
  • 130
4
votes
2 answers

Array merge on key of two associative arrays in php?

How can I merge these two array together? Array ( [0] => Array ( [id] => 5 [cnt] => 14 ) [1] => Array ( [id] => 8 [cnt] => 2 ) ) Array ( [0] => Array ( [id] => 8 …
Stefan
  • 359
  • 3
  • 17
4
votes
2 answers

Merging two multidimensional associative arrays

I'm chasing my tail trying to combine the results of two different queries to output in a template. I'm trying to merge the corresponding sub-arrays in model_data and entry_data to get desired_result. I will then iterate over desired_result and…
Judd Lyon
  • 538
  • 1
  • 4
  • 11
1 2
3
54 55