PHP array_diff() Function

Usage — The PHP array_diff() function is used to compute the difference of arrays by comparing the values of two (or more) arrays.

It has the following syntax:

PHP

array array_diff ( array $array1 , array $array2 [, array $... ] )

Here is an example of using array_diff():

PHP

$seniors = array("a"=>"Tim","b"=>"John","c"=>"Amanda","d"=>"Jill");
$juniors = array("e"=>"Jill","f"=>"amanda","g"=>"Tim");
$result=array_diff($seniors, $juniors);
print_r($result);

/* Output —
Array
(
    [b] => John
    [c] => Amanda
) */

Return Value — This function returns an array containing all the entries from array1 that are not present in any of the other arrays.

Additional Information — While using this function, you should remember that two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In other words, when the string representation is the same. Like other such functions, this function only checks one dimension of a n-dimensional array. You can still check deeper dimensions by using array_diff($array1[0], $array2[0]);.

PHP Version and Changelog — The array_diff() function is available in PHP 4 >= 4.0.1, PHP 5, PHP 7.

Relevant Functions — Other related PHP functions that you should know about are: array_diff_assoc() which computes the difference of arrays with additional index check, array_intersect() which computes the intersection of arrays and array_intersect_assoc() which computes the intersection of arrays with additional index check.

Go Directly To — usage, parameters, working examples or additional tips.

Parameters

array1

The array1 parameter is used to specify the array to compare from. This is a required parameter.

array2

The array2 parameter is used to specify an array to compare against. This is a required parameter.

The ... parameter is used to specify more arrays to compare against. This is an optional parameter.

Working Examples

Here are some examples of using the array_diff() function:

PHP

$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
/* Output —
Array
(
    [1] => blue
) */

Additional Tips

Here are some of the most upvoted tips taken from the comment section of the PHP manual:

  1. If you just need to know if two arrays’ values are exactly the same (regardless of keys and order), then instead of using array_diff(), this is a simple method:

    PHP

    function identical_values( $arrayA , $arrayB ) {
        sort( $arrayA );
        sort( $arrayB );
        return $arrayA == $arrayB;
    }
    $array1 = array( "red" , "green" , "blue" );
    $array2 = array( "green" , "red" , "blue" );
    $array3 = array( "red" , "green" , "blue" , "yellow" );
    $array4 = array( "red" , "yellow" , "blue" );
    $array5 = array( "x" => "red" , "y" =>  "green" , "z" => "blue" );
    
    // Output — bool(true)
    var_dump(identical_values( $array1 , $array2 ));
    
    // Output — bool(false)
    var_dump(identical_values( $array1 , $array3 ));
    
    // Output — bool(false)
    var_dump(identical_values( $array1 , $array4 ));
    
    // Output — bool(true)
    var_dump(identical_values( $array1 , $array5 ));
    

    The function returns TRUE only if the two arrays contain the same number of values and each value in one array has an exact duplicate in the other array. Everything else will return FALSE.

    My alternative method for evaluating if two arrays contain (all) identical values:

    PHP

    sort($a);
    sort($b);
    return $a == $b;
    

    May be slightly faster (10-20%) than this array_diff() method:

    PHP

    return ( count( $a ) == count( $b ) && !array_diff( $a , $b ) ? true : false );
    

    But only when the two arrays contain the same number of values and then only in some cases. Otherwise the latter method will be radically faster due to the use of a count() test before the array_diff().

    Also, if the two arrays contain a different number of values, then which method is faster will depend on whether both arrays need to be sorted or not. Two times sort() is a bit slower than one time array_diff(), but if one of the arrays have already been sorted, then you only have to sort the other array and this will be almost twice as fast as array_diff().

    Basically: 2 x sort() is slower than 1 x array_diff() is slower than 1 x sort().

    Suggested by - Jeppe Utzon

Further Reading

  1. You can read more about the PHP array_diff() function on PHP.net.

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

FacebookTwitterGoogle+
Go To Top