The array_udiff_uassoc() Function is an inbuilt function in PHP and is used for distinguishing between two or more array. The function computes the differences array by use of two user-defined functions with the additional index key. It compares both data and index by use of callback functions and returns the difference.
Syntax :
array_udiff_uassoc($arr1, $arr2, $arr3........nth array, value_Function, key_Function )
Parameters Used: This array_udiff_uassoc() function parameters are described below:
- $arr1, $arr2, $arr3, $arr4,…., $arrn : This represents the list of arrays which we want to distinguish.
- value_Function: This parameter represents the user-defined function. This user defined function will be used to compare values.
- key_Function: It is also a user-defined function. This user-defined function is used to compares arrays keys.
Note: Both comparison functions (value_Function, key_Function) returns an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.
Return Value: This function returns an array containing all values of the array $arr1 which are not present in any other arrays.
Some related functions:
- PHP | arr_diff() Function : Compute differences of an array.
- PHP | arr_udiff() Function: Compute differences of an array by used user-defined callback function and compare data.
- PHP | array_diff_assoc() Function : Compute differences of an array with additional index key.
- PHP | array_diff_uassoc() Function: This function compares both the keys and values between one or more arrays to and returns the elements from the first array which are not there in the rest of arrays.
- PHP | array_diff_key() Function: Compares the key of the first array of parameters with rest of the arrays and returns an array containing all the entries from $array1 that are not present in any of the other arrays.
Below programs illustrate the array_udiff_uassoc() Function.
Program 1:
PHP
<?php // PHP program to illustrate // array_udiff_uassoc() function // comparison function for array values function value_Function( $a , $b ) { if ( $a === $b ) { return 0; } return ( $a > $b ) ? 1 : -1; } // comparison function for array keys function key_Function( $a , $b ) { if ( $a === $b ) { return 0; } return ( $a > $b ) ? 1 : -1; } // array1 list for comparison. $arr1 = array ( "m" => "C lab" , "n" => "Java lab" , "o" => "C# lab" , "x" => "C++ lab" , "y" => "Ruby lab" , ); //array2 list for comparison. $arr2 = array ( "m" => "C lab" , "b" => "Java lab" , "c" => "C# lab" , "x" => "C++ lab" , "n" => "Ruby lab" , ); $result = array_udiff_uassoc ( $arr1 , $arr2 , "value_Function" , "key_Function" ); // print result. print_r( $result ); ?> |
Array ( [n] => Java lab [o] => C# lab [y] => Ruby lab )
Program: 2
PHP
<?php // PHP program to illustrate // array_udiff_uassoc() function // comparison function for array values function value_Function( $a , $b ) { if ( $a === $b ) { return 0; } return ( $a > $b ) ? 1 : -1; } // comparison function for array keys function key_Function( $a , $b ) { if ( $a === $b ) { return 0; } return ( $a > $b ) ? 1 : -1; } // array1 list for comparison. $arr1 = array ( "a" => "C lab" , "b" => "Java lab" , "c" => "C# lab" , "d" => "C++ lab" , "e" => "Ruby lab" , ); // array2 list for comparison. $arr2 = array ( "a" => "C lab" , "b" => "Java lab" , "c" => "C# lab" , "d" => "C++ lab" , "e" => "XML lab" , ); $arr3 = array ( "a" => "C lab" , "b" => "Java lab" , "c" => "C# lab" , "d" => "C++ lab" , "e" => "CSS lab" ); $arr4 = array ( "a" => "C lab" , "b" => "Java lab" , "c" => "C# lab" , "d" => "C++ lab" , "e" => "PHP lab" ); $result = array_udiff_uassoc ( $arr1 , $arr2 , $arr3 , $arr4 , "value_Function" , "key_Function" ); // print result. print_r( $result ); ?> |
Array ( [e] => Ruby lab )
Program: 3
PHP
<?php // PHP program to illustrate // array_udiff_uassoc() function // comparison function for array values function value_Function( $a , $b ) { if ( $a === $b ) { return 0; } return ( $a > $b ) ? 1 : -1; } // comparison function for array keys function key_Function( $a , $b ) { if ( $a === $b ) { return 0; } return ( $a > $b ) ? 1 : -1; } // array1 list for comparison. $arr1 = array ( "x" => "Geeks" , "y" => "for" , "z" => "Geeks" , ); // array2 list for comparison. $arr2 = array ( "x" => "Geeks" , "y" => "for" , "z" => "Geeks" , ); $result = array_udiff_uassoc ( $arr1 , $arr2 , "value_Function" , "key_Function" ); // print result. print_r( $result ); ?> |
Array ( )
Program: 4 Take three array (array1 and array2, array3) and using comparison function array_udiff_uassoc() Function. Where all three array has same index but values are different then return first array.
PHP
<?php // PHP program to illustrate // array_udiff_uassoc() function // comparison function for array values function value_Function( $a , $b ) { if ( $a === $b ) { return 0; } return ( $a > $b ) ? 1 : -1; } // comparison function for array keys function key_Function( $a , $b ) { if ( $a === $b ) { return 0; } return ( $a > $b ) ? 1 : -1; } // array1 list for comparison. $arr1 = array ( "a" => "C lab" , "b" => "Java lab" , "d" => "C# lab" , ); // array2 list for comparison. $arr2 = array ( "a" => "C " , "b" => "Java " , "d" => "C#" , ); // array3 list for comparison. $arr3 = array ( "a" => "Program" , "b" => "Code" , "d" => "Run" , ); $result = array_udiff_uassoc ( $arr1 , $arr2 , $arr3 , "value_Function" , "key_Function" ); // print result. print_r( $result ); ?> |
Array ( [a] => C lab [b] => Java lab [d] => C# lab )
Reference: http://php.net/manual/en/function.array-udiff-uassoc.php