Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to find the diffference of arrays in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 197
    Comment on it

    My previous blog Compute intersection of two or more arrays demonstrates about PHP functions which are used to compute intersection of two or more arrays. These functions return an array of common elements from first array which are also present in other arrays. This article demonstrates some functions which are used to compute the difference of arrays. These functions compare two or more arrays and return the non-matching elements from first array which are not present in other arrays. The list and details of these function are given below :

     

    1. array_diff()

    The array_diff() function is used to compute the difference of two or more arrays by comparing their values. This function compares all values of the first array with other arrays and return an array containing values from first array which are not matched with other array elements.

     

    Syntax : array_diff($array1, $array2, $array3)

    $array1 : Specify an array which will be compared with other arrays.(Required)

    $array2 : An array compared with array1.(Required)

    $array3 : An array compared with array1.(Optional)

     

    Result : This function returns an array containing values from $array1 which are not present in other arrays ($array2, $array3, ....).

     

    Note :

    1. Two elements of array's considered equals if their string representation is equal, i.e  (string)$arr1[0] ===(string)$arr2[0] .

    2. Generally, this function compares only the first dimension of n-dimensional array, To compare nth dimensional element use array_diff(arr1[n],arr2[n]).

     

    Example :

    <?php
    $array1 = array("a" => "orange", "b" => "apple", "c" => "mango", "d" => "pear");
    $array2 = array("b" => "peach", "a" => "orange", "c" => "mango", "d" => "banana");
    $result = array_diff($array1, $array2);
    print_r($result);
    ?>

    Output :

    Array ( [b] => apple [d] => pear )

     

     

    2. array_diff_key()

    array_diff_key() is also used for computing the difference of arrays by comparing keys.  This function compares the keys of the first array with others arrays and return an array containing elements of first array which are not present in other arrays. The main difference between array_diff() and array_diff_key() is array_diff() compares values of arrays while array_diff_key() compares keys of the arrays.

     

    Syntax : array_diff_key($arr1, $arr2, $arr3,....)

    $arr1 : Specify an array which will be compared with other arrays.(Required)

    $arr2 : An array which compared with $array1.(Required)

    $arr3 : Another array which compared with $array1.(Optional)

     

    Result : The function return an array after comparing keys of $arr1 with other arrays. The result array contains the elements of $arr1 whose key not matched with keys of other arrays.

     

    Example :

    <?php
    $array1 = array("a" => "orange", "1" => "apple", "c" => "mango", "2" => "pear");
    $array2 = array("b" => "peach", "a" => "orange", "c" => "mango", "d" => "banana");
    $result = array_diff_key($array1, $array2);
    print_r($result);
    ?>

    Output :

    Array ( [1] => apple [2] => pear )

     

     

    3. array_diff_assoc()

    array_diff_assoc() function computes the difference of two or more arrays by comparing both keys and values. This function compares both keys and values of the first array with other arrays and return an array of elements of first array which are not present in other arrays.

     

    Syntax : array_diff_assoc($arr1, $arr2, $arr3,.....)

    $arr1 : Specify an array which will be compared with other arrays.(Required)

    $arr2 : An array which compared with $array1.(Required)

    $arr3 : Another array which compared with $array1.(Optional)

     

    Result : This function returns an array containing keys and values from $arr1 that are not present in other arrays. The main difference between array_diff_assoc() and above defined functions is that, this function performs a comparison on the basis of both keys and values.

     

    Example :

    <?php
    $arr1=array("a"=>"apple","b"=>"orange","c"=>"pear","d"=>"mango");
    $arr2=array("a"=>"apple","c"=>"orange","g"=>"pear");
    $arr3=array("g"=>"pear","a"=>"apple","c"=>"pear");
    
    $result=array_diff_assoc($arr1,$arr2,$arr3);
    print_r($result);
    ?>

    Output :

    Array ( [b] => orange [d] => mango )

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: