PHP array_udiff() Function

PHP array_udiff() Function

The array_udiff() function in PHP calculates the difference of arrays by using a callback function for data comparison.

Syntax

array_udiff ( array $array1 , array $array2 [, array $... ], callable $value_compare_func )

Parameter

array1(required)- This array represents the first input array.

array2(required)- This array represents the second input array.

array3….(optional)- It represents more arrays to compare against.

value_compare_func(required)- This parameter represents a string that describe a callable comparison function. The comparison function returns an integer greater(<), equal to(=), or less than( >) than 0 if the first argument is greater(<), equal to(=), or less than( >) than the second argument.

Return

This function returns an array containing all the values of the first input array that does not exists in any of the other given arrays.

Example 1

 "mango", "b" => "Grapes", "c" => "Guava", "orange");
 //initializing the input array2
 $array2 = array("a" => "Mango", "c" => "GRAPES", "yellow", "orange");
 echo("The array_uintersect_assoc() returns:\n ");
 //returns an array containing all the values of array1 that are not present in all the other given arrays
 //will check only the values nor the keys 
 $differnce=array_udiff ($array1, $array2, "strcasecmp");//case-insensitive
 //will return Guava
 print_r($differnce);
 ?> 

Output

The array_uintersect_assoc() returns:
  Array
 (
     [c] => Guava
 ) 

Example 2

 "green", "2" => "brown", "3" => "blue", "red");
 //initializing the input array2
 $array2 = array("1" => "GREEN", "2" => "brown", "yellow", "red");
 echo("The array_udiff() returns:\n ");
 //returns an array containing all the values of array1 that are not present in all the other given arrays
 print_r(array_udiff ($array1, $array2, "strcasecmp"));
 ?> 

Output

The array_udiff() returns:
  Array
 (
     [3] => blue
 ) 

Example 3

$val2)?1:-1;
 }
 //input array1
 $array1=array("a"=>"PHP","b"=>"C#","c"=>"C++","d"=>"Python","e"=>"AI");
 //input array2
 $array2=array("a"=>"PHP","b"=>"C#","c"=>"Python");
 //input array2
 $array3=array("a"=>"PHP","c"=>"Java","d"=>"AI","e"=>"C++"); 
 // returns the difference of the three given arrays with the help of callable functions
 $diference=array_udiff ($array1,$array2,$array3,"myfunction");
 //will return null
 print_r($diference);
 ?> 

Output

Array
 (
 ) 

Example 4

$b)? 1 : -1;  
 } 
 // initializing the Input Array 1 
 $array1 = array(05=>"Reema", 10=>"raj", 30=>"Atul"); 
 // initializing the Input Array 2
 $array2 = array(05=>"Mahesh", 40=>"Smitha", 30=>"Atul"); 
 //returns the difference of the three given arrays with the help of value callable functions
 $difference=array_udiff ($array1, $array2, "myfunction"); 
 print_r($difference); 
 ?> 

Output

Array
 (
     [5] => Reema
     [10] => raj
     [30] => Atul
 ) 

Related Topics

Python Set pop() method

Python Set pop() method The set.pop() method in Python removes an arbitrary element (random item) from the set and returns the element removed. Syntax set.pop()  Parameter NA Return This method returns an arbitrary (random) element from the set. Example 1 #...

1 minute read.

PHP isset() function

PHP isset is used to check whether the variable is set or not.The PHP isset() function returns false if variable contains a NULL value. Syntax: isset(variable1, variable2….) Presentation of PHP isset: In...

2 minutes read.

PHP convert_uudecode() Function

PHP convert_uudecode() Function The convert_uudecode() function in PHP decodes a uuencoded string. Syntax convert_uudecode ( string $data ) Parameter data- This parameter represents the uuencoded data. Return This function returns the decoded data as a string or FALSE on failure. Example 1 <?php // initializing the string $str...

1 minute read.

PHP key() Function

PHP key() Function The key() function in PHP fetches a key from an array and returns the index element of the current array position. Syntax key ( array $array ) Parameter array(required)- This parameter represents the input array. Return This function returns the key of the...

1 minute read.

PHP empty() Function

PHP empty() Function The empty() function in PHP determines whether the specified variable is empty or not. The specified variable is considered empty if it does not exist or if its value is equal to FALSE. This function...

1 minute read.

PHP natsort() Function

PHP natsort() Function The natsort() function in PHP Sort an array using a case insensitive "natural order" algorithm while maintaining the keys. Syntax natsort ( array &$array ) Parameter array(required)- This parameter represents the input array. Return This parameter returns a Boolean value TRUE on success or FALSE on...

1 minute read.

PHP intval() Function

The intval() Function in PHP is used to get the integer value for the given variable. This function should not be used on objects. Else it will emit an E_NOTICE level error and return 1. Syntax intval...

1 minute read.

PHP strtr() Function

PHP strtr() Function The strtr() function in PHP translates or replaces certain characters in a string. Syntax strtr ( string $str , string $from , string $to ) or strtr ( string $str , array $replace_pairs ) Parameter str(required)- This parameter specifies the string to translate from(required)- This parameter represents the string being translated to the parameter ‘to’. to(required)- It specifies what...

1 minute read.

PHP stristr() Function

PHP stristr() Function The stristr() function in PHP finds the first occurrence of a string and returns the matched substring. It is a case-insensitive function. Syntax stristr ( string $haystack , mixed $needle [, bool $before_needle ] ) Parameter haystack(required)- This parameter represents the input string. needle(required)- This parameter...

2 minutes read.

PHP unset() Function

PHP unset() Function The unset() function in PHP is used to unset a given variable. This function destroys the specified variables. If this function is called inside any user-defined function, then it unsets the value associated with...

1 minute read.

PHP is_float() Function

The PHP is_float() function in PHP is used to  find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter var is...

1 minute read.

PHP addcslashes() Function

The addcslashed() function in PHP is used to add quote string with slashes in a C style. It is case-sensitive function.  Syntax addcslashes (str, charlist ) Parameter str(required)- This parameter represents the string to be escaped. charlist(required)-...

1 minute read.

PHP array_intersect_ukey() function

The array_intersect_ukey () function in PHP is used to compute the intersection of arrays with the help of a callable function on the keys for comparison. It returns an array containing all the...

1 minute read.

PHP array_combine() Function

PHP array_combine() Function The array_combine() function in PHP is used to create an array by combining two arrays i.e., one array for keys and another array for its values. Syntax array_combine ( array $keys , array $values ) Parameter keys(required)- This parameter represents an array which acts...

1 minute read.

PHP floatval() Function

PHP floatval() Function The floatval() function in PHP is used to give the float value of a variable. Syntax floatval ( mixed $var ) Parameter var(required)- This parameter represents the name of the variable that is to be converted. Return This function...

1 minute read.

PHP array_count_values() Function

PHP array_count_values() Function The array_count_values() function in PHP counts all the values present in the specified array and returns an array using the values of the array as keys and their frequency in the array as values. Syntax array_count_values ( array $array ) Parameter array(required)- This parameter represents the...

1 minute read.

PHP array_replace() Function

PHP array_replace() Function The array_replace() function in PHP replaces the elements from passed arrays into the first array with values having the same keys in each of the following arrays. Syntax array_replace ( array $array1 [, array $... ] ) Parameter array(required)- This parameter represents...

1 minute read.

PHP Explode() Function

PHP Explode The PHP explode( ) is used to break a string into an array. The PHP explode( ) allow us to break a string into a smaller text with each break...

1 minute read.

WordPress Theme

Writing our WordPress Theme is relatively simple. Requirements: Before we begin, we need to know about the following set of requirements. We need to have full knowledge of WordPress setup, which is either...

2 minutes read.

PHP Constant

PHP constant is an identifier(name) for a simple value. The value does not change during the script. It starts with a letter or underscore not $ sign. The define()function is...

1 minute read.