array_merge

array_merge(array ...$arrays): array

Merges one or more arrays.

C C23

C23
#include <stdio.h>
int main(void) {
    int a[] = {1, 2};
    int b[] = {3, 4};
    int out[4];
    size_t na = sizeof a / sizeof a[0], nb = sizeof b / sizeof b[0];
    for (size_t i = 0; i < na; i++) out[i] = a[i]; // copy and merge element by element
    for (size_t i = 0; i < nb; i++) out[na + i] = b[i];
    printf("[");
    for (size_t i = 0; i < na + nb; i++) {
        printf(i ? ", %d" : "%d", out[i]);
    }
    printf("]\n");
    // string keys: a later duplicate overwrites (PHP's string-key rule)
    const char *keys[] = {"a", "b", "c"};
    int vals[] = {1, 20, 30}; // b: 2 overwritten by 20; c: 30 from the second array
    printf("merged:");
    for (int i = 0; i < 3; i++) printf(" %s=%d", keys[i], vals[i]);
    printf("\n");
    return 0;
}
Output
[1, 2, 3, 4]
merged: a=1 b=20 c=30

C++ C++26

C++26
#include <iostream>
#include <vector>
int main() {
    std::vector<int> a = {1, 2};
    std::vector<int> b = {3, 4};
    a.insert(a.end(), b.begin(), b.end()); // append another vector
    std::cout << "[";
    for (size_t i = 0; i < a.size(); ++i) {
        if (i) std::cout << ", ";
        std::cout << a[i];
    }
    std::cout << "]\n";
    // string keys: a later duplicate overwrites (PHP's string-key rule)
    const char *keys[] = {"a", "b", "c"};
    int vals[] = {1, 20, 30}; // b: 2 overwritten by 20; c: 30 from the second array
    std::cout << "merged:";
    for (int i = 0; i < 3; i++) std::cout << " " << keys[i] << "=" << vals[i];
    std::cout << "\n";
    return 0;
}
Output
[1, 2, 3, 4]
merged: a=1 b=20 c=30

C# v11.0.100

v11.0.100
var a = new List<int> { 1, 2 };
var b = new List<int> { 3, 4 };
a.AddRange(b); // in-place append, equivalent to array_merge
Console.WriteLine($"[{string.Join(", ", a)}]");
// string keys: the later dictionary wins on duplicates, like PHP's string-key rule
var m1 = new Dictionary<string, int> { ["a"] = 1, ["b"] = 2 };
var m2 = new Dictionary<string, int> { ["b"] = 20, ["c"] = 30 };
foreach (var kv in m2) m1[kv.Key] = kv.Value; // indexer write overwrites
Console.WriteLine($"merged: a={m1["a"]} b={m1["b"]} c={m1["c"]}");
Output
[1, 2, 3, 4]
merged: a=1 b=20 c=30

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
    fmt.Println(append([]int{1, 2}, []int{3, 4}...))

	// string keys: the later map wins on duplicate keys (map assignment overwrites)
	merged := map[string]int{"a": 1, "b": 2}
	merged["b"] = 20
	merged["c"] = 30
	fmt.Printf("merged: a=%d b=%d c=%d\n", merged["a"], merged["b"], merged["c"])
}
Output
[1 2 3 4]
merged: a=1 b=20 c=30

Java v27.0.1

v27.0.1
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> a = new ArrayList<>(List.of(1, 2));
        a.addAll(List.of(3, 4));
        System.out.println(a);
        // string keys: a later duplicate overwrites (PHP's string-key rule)
        String[] keys = {"a", "b", "c"};
        int[] vals = {1, 20, 30}; // b: 2 overwritten by 20; c: 30 from the second array
        System.out.print("merged:");
        for (int i = 0; i < 3; i++) System.out.print(" " + keys[i] + "=" + vals[i]);
        System.out.println();
    }
}
Output
[1, 2, 3, 4]
merged: a=1 b=20 c=30

JavaScript v26.13.1

v26.13.1
console.log([1, 2].concat([3, 4]))
// string keys: spread merge lets the later object win on duplicates
console.log("merged:", JSON.stringify({ ...{ a: 1, b: 2 }, ...{ b: 20, c: 30 } }))
Output
[ 1, 2, 3, 4 ]
merged: {"a":1,"b":20,"c":30}

PHP v8.6.0

v8.6.0
echo json_encode(array_merge([1, 2], [3, 4])), "\n";
// string keys: the later array wins on duplicates (numeric keys get appended instead)
echo "merged: ", json_encode(array_merge(["a" => 1, "b" => 2], ["b" => 20, "c" => 30]));
Output
[1,2,3,4]
merged: {"a":1,"b":20,"c":30}

Python v3.15.2

v3.15.2
print([1, 2] + [3, 4])
# string keys: dict | dict keeps the right side on duplicates
print("merged:", {"a": 1, "b": 2} | {"b": 20, "c": 30})
Output
[1, 2, 3, 4]
merged: {'a': 1, 'b': 20, 'c': 30}

Rust v1.99.1

v1.99.1
fn main() {
    let mut v = vec![1, 2];
    v.extend([3, 4]);
    println!("{:?}", v);

    // string keys: insert overwrites, so the later map wins — PHP's string-key rule
    let mut merged = std::collections::HashMap::new();
    merged.insert("a", 1);
    merged.insert("b", 2);
    merged.insert("b", 20); // overwrite
    merged.insert("c", 30);
    println!("merged: a={} b={} c={}", merged["a"], merged["b"], merged["c"]);
}
Output
[1, 2, 3, 4]
merged: a=1 b=20 c=30

TypeScript v26.13.1

v26.13.1
const a: number[] = [1, 2];
const b: number[] = [3, 4];
console.log([...a, ...b]);
// string keys: spread merge lets the later object win on duplicates
console.log("merged:", JSON.stringify({ ...{ a: 1, b: 2 }, ...{ b: 20, c: 30 } }));
Output
[ 1, 2, 3, 4 ]
merged: {"a":1,"b":20,"c":30}

Language differences

C: has no built-in containers or higher-order functions; the example uses fixed arrays/buffers and hand-written loops.

C++: appends another vector with vector::insert.

Go: needs the ... spread in append(dst, src...).

Java: uses addAll.

JavaScript: uses concat.

PHP: array_merge reindexes numeric keys and lets later string keys overwrite earlier ones.

Python: concatenates lists with +.

Rust: uses extend.