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_intersect()関数:他の配列との共通値の抽出する

0
Posted at

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

(PHPバージョン4.0.1+)

定義と使い方

array_intersect()関数は、基準となる配列の中から、1つ以上の他の配列にも同様に存在する共通(intersect、積集合)の値を抽出し、配列として返します。
この関数は配列間の共通の値を比較する際に活用され、主に以下のような状況で使用されます。

  • 二つの配列に共通して存在する値の抽出
  • ユーザーが入力した値が既に存在するかどうかの確認
  • 複数のデータソースに同時に存在するデータのみを選別

特徴

  • この関数は、キー(Key)ではなく値(Value)を基準に配列の共通要素を抽出します。その際、緩やかな比較(==)を使用するため、データの型が異なっていても内容が同じであれば一致するものと見なされます。
  • 返された配列のキーは、基準となる配列のキーがそのまま保持されます。

基本例

# インデックス配列同士の比較

---- PHP ----
/* インデックス配列同士の比較 */

$index_array1 = [1, 2, 3, 4, 5]; // 比較基準となる配列
$index_array2 = [2, 3, 4, 6, 7];

$index_unique_value = array_intersect($index_array1, $index_array2);

print_r($index_unique_value);

---- 出力 ----
Array
(
    [1] => 2
    [2] => 3
    [3] => 4
)

# 連想配列同士の比較

---- PHP ----
/* 連想配列同士の比較 */

$assoc_array1 = [  // 比較基準となる配列
	'a' => 'apple',
	'b' => 'banana',
	'c' => 'carrot',
	'd' => 'date'
];

$assoc_array2 = [
	'x' => 'carrot',
	'b' => 'grape',
	'y' => 'apple'
];

$assoc_unique_value = array_intersect($assoc_array1, $assoc_array2);

print_r($assoc_unique_value);

---- 出力 ----
Array
(
    [a] => apple
    [c] => carrot
)

# インデックス配列と連想配列の比較

---- PHP ----
/* インデックス配列と連想配列の比較 */

$arr_idx = ['a', 'b', 'c'];  // 比較基準となる配列

$arr_assoc = [
    'x' => 'b',
    'y' => 'c',
    'z' => 'd'
];

$assoc_unique_value = array_intersect($arr_idx, $arr_assoc);

print_r($assoc_unique_value);

---- 出力 ----
Array
(
    [1] => b
    [2] => c
)

構文

array_intersect(array $array_1, $array_2, $array_3, ...): array

戻り値

比較基準となる配列と、1つ以上の他の配列との共通値を探し、配列として返します。
この際、返される配列のキーは、比較基準となる配列のキーがそのまま保持されます。ただし、インデックス配列(数値キーのみの配列)の場合、キーは 0 から振り直されます。
もし、比較する他の配列と、比較基準となる最初の配列の両方に共通して存在する値がない場合には、空の配列を返します。

活用例

# データベースの重複データの抽出

$userIdsFromDatabase = [1, 2, 3, 4, 5];
$selectedUserIds = [2, 4, 6, 8, 10];

$commonUserIds = array_intersect($userIdsFromDatabase, $selectedUserIds);
print_r($commonUserIds); // Array ( [1] => 2 [3] => 4 )

# フォーム入力値のバリデーション

$submittedTags = ['php', 'javascript', 'html'];
$allowedTags = ['html', 'css', 'javascript', 'python'];

$validTags = array_intersect($submittedTags, $allowedTags);
print_r($validTags); // Array ( [2] => javascript [1] => html )

# ユーザー権限の管理

$adminPermissions = ['read', 'write', 'delete', 'update'];
$userPermissions = ['read', 'write'];

$allowedPermissions = array_intersect($adminPermissions, $userPermissions);
print_r($allowedPermissions); // Array ( [0] => read [1] => write )

# 商品在庫の管理

$availableProducts = ['product1', 'product2', 'product3', 'product4'];
$selectedProducts = ['product2', 'product4', 'product5'];

$inStockProducts = array_intersect($availableProducts, $selectedProducts);
print_r($inStockProducts); // Array ( [1] => product2 [3] => product4 )

参考資料

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?