58
60

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

配列結合演算子, array_merge, array_replace を徹底比較

Last updated at Posted at 2015-03-16

導入

PHPで配列を結合するための方法は幾らかありますが、それぞれ微妙に挙動が異なるので、ここでは違いが分かりやすいようにまとめてみます。

比較

早見表

array_mergearray_merge_recursiveの挙動にはかなりクセがあるのでご注意を。

|手段 \ キー |未定義整数|未定義文字列|重複整数|重複文字列|
|:---:|:---:|:---:|:---:|:---:|:---:|
|配列結合演算子 +|追加|追加|無視|無視|
|array_merge|連番に直して追加|追加|連番に直して追加|上書き|
|array_replace|追加|追加|上書き|上書き|
|array_merge_recursive|連番に直して追加|追加|連番に直して追加|可能な場所まで相互に再帰

双方を配列にキャスト

array_mergeを用いて結合|
|array_replace_recursive|追加|追加|可能な場所まで相互に再帰

上書き|可能な場所まで相互に再帰

上書き|

PHP関数としての表現

自信があまり無いので間違い等ありましたらご指摘ください。

配列結合演算子 +
function plus(array $base, array $another) {
    foreach ($another as $key => $value) {
        if (!array_key_exists($key, $base)) {
            $base[$key] = $value;
        }
    }
    return $base;
}
array_merge
function array_merge(array $base, array ...$others) {
    foreach ($others as $other) {
        foreach ($other as $key => $value) {
            if (is_int($key)) {
                $base[] = $value;
            } else {
                $base[$key] = $value;
            }
        }
    }
    return $base;
}
array_replace
function array_replace(array $base, array ...$others) {
    foreach ($others as $other) {
        foreach ($other as $key => $value) {
            $base[$key] = $value;
        }
    }
    return $base;
}
array_merge_recursive
function array_merge_recursive(array $base, array ...$others) {
    foreach ($others as $other) {
        foreach ($other as $key => $value) {
            if (is_int($key)) {
                $base[] = $value;
            } elseif (array_key_exists($key, $base)) {
                if (is_array($base[$key]) && is_array($value)) {
                    $base[$key] = array_merge_recursive($base[$key], $value);
                } else {
                    $base[$key] = array_merge((array)$base[$key], (array)$value);
                }
            } else {
                $base[$key] = $value;
            }
        }
    }
    return $base;
}
array_replace_recursive
function array_replace_recursive(array $base, array ...$others) {
    foreach ($others as $other) {
        foreach ($other as $key => $value) {
            if (array_key_exists($key, $base) && is_array($base[$key]) && is_array($value)) {
                $base[$key] = array_replace_recursive($base[$key], $value);
            } else {
                $base[$key] = $value;
            }
        }
    }
    return $base;
}
58
60
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
58
60

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?