LoginSignup
0
0

More than 3 years have passed since last update.

[PHP] 2つ以上の配列の中の共通の値を取得する

Posted at

概要

二つの配列を比較して同じ値だけを取り出したい場合の方法をメモします。

方法

  • array_intersect()を使うことで共通の値だけを取り出すことが可能
<?php
$array1 = [1, 2, 3];
$array2 = [2, 3, 4];

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

print_r($result);

出力結果

Array
(
    [1] => 2
    [2] => 3
)

3つ以上を比較する場合は第三引数以降に配列を追加することで可能

$result = array_intersect($array1, $array2, $array3, $array4);
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