Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. array_intersect() / array_diff()

array_intersect() / array_diff() Since: PHP 4(2000)

Functions for retrieving common elements or differences between multiple arrays. Supports both value-based and key-based comparison.

Syntax

// Returns elements with values common to all arrays.
array_intersect(array1, array2, ...);

// Returns elements in the first array that are not in the other arrays.
array_diff(array1, array2, ...);

// Returns elements with keys common to all arrays.
array_intersect_key(array1, array2, ...);

// Returns elements whose keys are in the first array but not in the other arrays.
array_diff_key(array1, array2, ...);

Function List

FunctionDescription
array_intersect($array1, $array2, ...)Compares array values and returns elements whose values exist in all arrays. Keys from the first array are preserved.
array_diff($array1, $array2, ...)Compares array values and returns elements that exist in the first array but not in the other arrays.
array_intersect_key($array1, $array2, ...)Compares array keys and returns elements from the first array whose keys are common to all arrays.
array_diff_key($array1, $array2, ...)Compares array keys and returns elements from the first array whose keys do not exist in the other arrays.

Sample Code

<?php
$team_a = ['PHP', 'JavaScript', 'Python', 'Ruby'];
$team_b = ['JavaScript', 'Python', 'Go', 'Rust'];

// Get the skills common to both teams.
$common = array_intersect($team_a, $team_b);
print_r($common); // Returns 'JavaScript' and 'Python'.

// Get the skills in team A that are not in team B.
$only_a = array_diff($team_a, $team_b);
print_r($only_a); // Returns 'PHP' and 'Ruby'.

// Compare keys in associative arrays.
$old_settings = ['theme' => 'dark', 'lang' => 'ja', 'font_size' => 14];
$new_settings = ['theme' => 'light', 'lang' => 'en', 'sidebar' => true];

// Get elements with keys common to both arrays.
$common_keys = array_intersect_key($old_settings, $new_settings);
print_r($common_keys); // Returns the old settings values for 'theme' and 'lang'.

// Get keys in the old settings that are not in the new settings.
$removed = array_diff_key($old_settings, $new_settings);
print_r($removed); // Returns the removed setting 'font_size'.

// Get keys in the new settings that are not in the old settings.
$added = array_diff_key($new_settings, $old_settings);
print_r($added); // Returns the added setting 'sidebar'.

Notes

Both array_intersect() and array_diff() compare array values, and the keys from the first array are preserved in the returned array. Since the keys may be non-contiguous, use array_values() to reindex if needed.

Both array_intersect_key() and array_diff_key() compare by key. The values in the returned array come from the first array — values from subsequent arrays are not used. These functions are useful for detecting configuration differences and merging data.

To compare both values and keys at the same time, use array_intersect_assoc() or array_diff_assoc(). To search for a specific value within an array, in_array() / array_search() is the appropriate choice.

If you find any errors or copyright issues, please .