PHP array_diff() Function

What is PHP array_diff() Function?

If you want to know what values exist in one array and that doesn’t exist in other arrays (one or more arrays), use array_diff() function. This difference is made in respect to values only. So, one element is considered exist if that element with same value is found in another array and the keys may be same or different.

Syntax:

array_diff(array1, array2, array3, …)

Parameters:

The Function has 2 required parameters and any number of optional parameters-

array1 (Required): This is the first array whose elements the function checks whether exist in other arrays or not.

array2 (Required): It is another array whose elements the function compares with the first array. Check example 1.

array3, … (Optional): It is one or more arrays whose elements the function compares with the first array. Check example 2.

Return Values:

The function returns an array which contains elements that exist in first array and don’t exist in other arrays.

  • If values and keys are same but case are different in the comparing arrays, the function considers it non-exists. Check example 4.

Examples:

Example 1:

<pre>
<?php
$array1 = ['c' => 'CSS', 'h' => 'HTML',  'j' => 'JavaScript', 'p' => 'PHP', 'c' => 'C#', 'g' => 'Go', 'j' => 'Java', 'n' => 'NoSQL'];
$array2 = ['c' => 'CSS', 'h' => 'HTML',  'j' => 'JavaScript', 'P' => 'PHP'];
$last_array = array_diff($array1, $array2);
print_r($last_array);
?>
</pre>

Output:

Array
(
    => C#
    [j] => Java
    [g] => Go
    [n] => NoSQL
)

Example 2:

<pre>
<?php
$array1 = ['c' => 'CSS', 'h' => 'HTML',  'j' => 'JavaScript', 'p' => 'PHP', 'c' => 'C#', 'g' => 'Go', 'j' => 'Java', 'n' => 'NoSQL'];
$array2 = ['c' => 'CSS', 'h' => 'HTML',  'j' => 'JavaScript', 'p' => 'PHP'];
$array3 = ['c' => 'CSS', 'c' => 'C#', 'g' => 'Go', 'J' => 'Java'];
$last_array = array_diff($array1, $array2, $array3);
print_r($last_array);
?>
</pre>

Output:

Array
(
    [n] => NoSQL
)

Example 3:

<pre>
<?php
$array1 = ['c' => 'CSS', 'j' => 'JavaScript'];
$array2 = ['c' => 'CSS', 'j' => 'Javascript'];
$last_array = array_diff($array1, $array2);
print_r($last_array);
?>
</pre>

Output:

Array
(
    [j] => JavaScript
)

Example 4:

<pre>
<?php
$array1 = ['j' => 'JavaScript', 'c' => ['C', 'C++', 'C#'], 'p' => 'PHP'];
$array2 = ['c' => 'CSS', 'c' => ['C', 'C++']];
$last_array = array_diff($array1['c'], $array2['c']);
print_r($last_array);
?>
</pre>

Output:

Array
(
    [third] => C#
)

Example 5:

<pre>
<?php
$array1 = ['first' => 'CSS', 'second' => '1'];
$array2 = ['first' => 'JavaScript', 'second' => 1];
$last_array = array_diff($array1, $array2);
print_r($last_array);
?>
</pre>

Output:

Array
(
    [first] => CSS
)

Example 6:

<pre>
<?php
$array1 = ['CSS', 'HTML', 'JavaScript', 'PHP', 'PHP', 'CSS', 'NoSQL'];
$array2 = ['HTML', 'JavaScript', 'jQuery'];
$last_array = array_diff($array1, $array2);
print_r($last_array);
?>
</pre>

Output:

Array
(
    [0] => CSS
    [3] => PHP
    [4] => PHP
    [5] => CSS
    [6] => NoSQL
)

Notes on array_diff() Function:

  • The function checks elements in one-dimension. If you want to compare elements inside inner levels of multidimensional arrays, you can mention those as the parameters in the function. Check example 4.
  • Two values are considered equal if their string representation are same that is (string) $elem1 === (string) $elem2. Check example 5.
  • Repeating elements also repeats in the resultant array. Check example 6.

PHP Version Support:

PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8

Summary: PHP array_diff() Function

array_diff() is a built-in array function which is a very effective function to compare elements between arrays.

Reference:

https://www.php.net/manual/en/function.array-diff.php