0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PHP array_merge()関数:配列同士を結合する

0
Last updated at Posted at 2026-08-11

😄この投稿は、codingeverybody.jpのコンテンツをもとに作成しています。

定義と使い方

array_merge()関数は、1つ以上の配列を順番に結合し、新しい配列を作成します。
この関数は、複数の配列を1つにまとめる必要がある場合に便利に使用できます。

特徴

  • 複数の配列を任意の順序で結合することができます。
  • 既存の配列は変更されず、結合結果を含む新しい配列が返されます。
  • 各配列の要素は順番に連結されます。

基本例

/* インデックス配列の結合 */
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];

// 2つの配列を結合
$result = array_merge($array1, $array2);

print_r($result);
/*
出力:
    Array
    (
        [0] => 1
        [1] => 2
        [2] => 3
        [3] => 4
        [4] => 5
        [5] => 6
    )
*/

/* 連想配列の結合 */
$assoc_array1 = ['a' => 'red', 'b' => 'green'];
$assoc_array2 = ['c' => 'blue', 'd' => 'yellow'];

// 2つの配列を結合
$assoc_result = array_merge($assoc_array1, $assoc_array2);

print_r($assoc_result);
/*
出力:
    Array
    (
        [a] => red
        [b] => green
        [c] => blue
        [d] => yellow
    )
*/

構文

array_merge(array array1, array2, array3, ...): array

戻り値

  • 結合する配列が連想配列(文字列キーを持つ配列)の場合、同じ文字列キーが存在すると、後に指定された配列の値が前の配列の値を上書きします。
  • 結合する配列が数値キー(インデックス配列)を持つ場合、値は上書きされず、結果の配列の末尾に追加されます。
  • 結果の配列の数値キーは、0から始まる連続した整数キーに再構成されます。

#文字列キーの重複処理

$array1 = ['a' => 'apple', 'b' => 'banana'];
$array2 = ['b' => 'blueberry', 'c' => 'cherry'];

$result = array_merge($array1, $array2);

print_r($result);
/*
出力:
    Array
    (
        [a] => apple
        [b] => blueberry
        [c] => cherry
    )
*/

#数値キー(インデックス)の重複処理

$array1 = [0 => 'apple', 1 => 'banana'];
$array2 = [1 => 'blueberry', 2 => 'cherry'];

$result = array_merge($array1, $array2);

print_r($result);
/*
出力:
    Array
    (
        [0] => apple
        [1] => banana
        [2] => blueberry
        [3] => cherry
    )
*/

#文字列キーと数値キーの重複処理

$array1 = ['apple', 'banana', 'color' => 'red'];
$array2 = ['green', 'orange', 'color' => 'orange', 'shape' => 'circle'];

$result = array_merge($array1, $array2);
print_r($result);
/*
出力:
    Array
    (
        [0] => apple
        [1] => banana
        [color] => orange
        [2] => green
        [3] => orange
        [shape] => circle
    )
*/

#多次元連想配列の結合

// 1つ目の配列
$array1 = [
    'name' => 'John Doe',
    'age' => 30,
    'address' => [
        'street' => '123 Main Street',
        'city' => 'Anytown',
        'state' => 'CA'
    ]
];

// 2つ目の配列
$array2 = [
    'phone' => '123-456-7890',
    'email' => 'johndoe@example.com'
];

// 2つの配列を結合
$result = array_merge($array1, $array2);

// 結果を出力
print_r($result);
/*
出力:
    Array
    (
        [name] => John Doe
        [age] => 30
        [address] => Array
            (
                [street] => 123 Main Street
                [city] => Anytown
                [state] => CA
            )
        [phone] => 123-456-7890
        [email] => johndoe@example.com
    )
*/

参考資料

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?