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
302
votes
6 answers

PHP: merge two arrays while keeping keys instead of reindexing?

How can I merge two arrays (one with string => value pairs and another with int => value pairs) while keeping the string/int keys? None of them will ever overlap (because one has only strings and the other has only integers). Here is my current code…
Garrett
  • 11,451
  • 19
  • 85
  • 126
211
votes
35 answers

How to merge dictionaries of dictionaries?

I need to merge multiple dictionaries, here's what I have for instance: dict1 = {1:{"a":{A}}, 2:{"b":{B}}} dict2 = {2:{"c":{C}}, 3:{"d":{D}}} With A B C and D being leaves of the tree, like {"info1":"value", "info2":"value2"} There is an unknown…
fdhex
  • 2,212
  • 2
  • 16
  • 13
134
votes
2 answers

Array_merge versus +

When I use array_merge() with associative arrays I get what I want, but when I use them with numerical key arrays the keys get changed. With + the keys are preserved but it doesn't work with associative arrays. I don't understand how this works, can…
Elly
  • 1,343
  • 2
  • 9
  • 4
131
votes
6 answers

Prepend associative array elements to an associative array

Is it possible to prepend an associative array with literal key=>value pairs? I know that array_unshift() works with numerical keys, but I'm hoping for something that will work with literal keys. As an example I'd like to do the following: $array1 =…
Colin Brock
  • 21,267
  • 9
  • 46
  • 61
69
votes
4 answers

PHP - How to merge arrays inside array

How to merge n number of array in php. I mean how can I do the job like : array_merge(from : $result[0], to : $result[count($result)-1]) OR array_merge_recursive(from: $result[0], to : $result[count($result) -1]) Where $result is an array with…
Lekhnath
  • 4,532
  • 6
  • 36
  • 62
59
votes
6 answers

Merging arrays with the same keys

In a piece of software, I merge two arrays with array_merge function. But I need to add the same array (with the same keys, of course) to an existing array. The problem: $A = array('a' => 1, 'b' => 2, 'c' => 3); $B = array('c' => 4, 'd'=> 5); …
kuzey beytar
  • 3,076
  • 6
  • 37
  • 46
51
votes
8 answers

PHP array_merge with numerical keys

How can make it so array_merge() overwrites two keys with different values but same key index from two arrays? for example, merging: [0] => 'whatever' with [0] => 'whatever', [1] => 'a', [2] => 'b' should produce [0] => 'whatever', [1] => 'a', [2]…
Alex
  • 66,732
  • 177
  • 439
  • 641
31
votes
2 answers

php array_merge_recursive preserving numeric keys

I would simply like to merge $a = array("59745506"=>array("up" => 0,)); $b = array("59745506"=>array("text" => "jfrj")); $c = array_merge_recursive_new($a, $b); result: Array ( [0] => Array ( [up] => 0 ) [1] =>…
user1125394
27
votes
7 answers

php array_merge associative arrays

I'm trying to prepend an item to the beginning of an associative array. I figured the best way to do this is to use array_merge, but I'm having some odd consequences. I get the id and Name of products from a mysql database, and it gets returned as…
stephenbayer
  • 12,373
  • 15
  • 63
  • 98
22
votes
7 answers

PHP Deep Extend Array

How can I do a deep extension of a multi dimensional associative array (for use with decoded JSON objects). I need the php equivalent of jQuery's $.extend(true, array1, array2) with arrays instead of JSON and in PHP. Here's an example of what I need…
Sabrina Leggett
  • 9,079
  • 7
  • 47
  • 50
17
votes
5 answers

How can I interleave two arrays?

If I have two arrays e.g let one = [1,3,5] let two = [2,4,6] I would like to merge/interleave the arrays in the following pattern [one[0], two[0], one[1], two[1] etc....] //prints [1,2,3,4,5,6] let comibned = mergeFunction(one,…
Neil Horton
  • 707
  • 6
  • 13
14
votes
5 answers

array_merge & array_unique

Is there an array function in PHP that somehow does array_merge, comparing the values, ignoring the keys? I think that array_unique(array_merge($a, $b)) works, however I believe there must be a nicer way to do this. eg. $a = array(0 => 0, 1 => 1, 2…
ptmr.io
  • 2,115
  • 4
  • 22
  • 34
13
votes
7 answers

Array-Merge on an associative array in PHP

How can i do an array_merge on an associative array, like so: Array 1: $options = array ( "1567" => "test", "1853" => "test1", ); Array 2: $option = array ( "none" => "N/A" ); So i need to array_merge these two but when i do i get this (in…
benhowdle89
  • 36,900
  • 69
  • 202
  • 331
13
votes
2 answers

PHP : multidimensional array merge recursive

I need to merge those two arrays: $ar1 = array("color" => array("red", "green"), "aa"); $ar2 = array("color" => array( "green", "blue"), "bb"); $result = array_merge_recursive($ar1, $ar2); Expected output: [ 'color' => [ (int) 0 =>…
hjrshng
  • 1,675
  • 3
  • 17
  • 30
12
votes
1 answer

How to merge multidimensional arrays while preserving keys?

Is there a way for these arrays $array1 = array( '21-24' => array( '1' => array("...") ) ); $array2 = array( '21-24' => array( '7' => array("..."), ) ); $array3 = array( '25 and over' => array( '1' =>…
developarvin
  • 4,940
  • 12
  • 54
  • 100
1
2 3
54 55