PHP array_uintersect() Function

PHP array_uintersect() Function

The array_uintersect() function in PHP calculates the intersection of arrays by comparing the data by a callback function. It returns an array containing all the values of the first input array that are present in all the arguments.

Syntax

array_uintersect ( 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 are present in all the given arrays.

Example 1

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

Output

The array_uintersect() returns:
  Array
 (
     [1] => green
     [2] => brown
     [4] => red
 ) 

Example 2

<?php
 //initializing the input array1
 $array1 = array("a" => "mango", "b" => "Grapes", "c" => "Guava", "orange");
 //initializing the input array2
 $array2 = array("a" => "Mango", "b" => "GRAPES", "yellow", "orange");
 echo("The array_uintersect_assoc() returns:\n ");
 //returns an array containing all the values of array that exists in all the other given arrays
 $intersection=array_uintersect($array1, $array2, "strcasecmp");//case-insensitive
 //will return mango, Grapes and orange
 print_r($intersection);
 ?> 

Output

Array
 (
     [a] => mango
     [b] => Grapes
     [0] => orange
 ) 

Example 3

<?php 
 //initializing the callable function
 function compare_function($a, $b) 
 { 
   if ($a===$b) 
   { 
     return 1;  
   } 
   return ($a>$b)? 1: 0; 
 } 
 // initializing the Input Array 1 
 $array1 = array(05=>"Reema", 10=>"raj", 30=>"Atul"); 
 // initializing the Input Array 2
 $array2 = array(20=>"Mahesh", 40=>"Smitha", 30=>"Atul");  
 // Computes the intersection of arrays  
 $intersection=array_uintersect($array1, $array2, "compare_function"); 
 print_r($intersection); 
 ?> 

Output

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

Related Topics

PHP For loop with Example

For loop in PHP : The for loop is the most potent loop, it combines the abilities to set up variables as we enter the loop, test for conditions while iterating...

3 minutes read.

PHP bin2hex() Function | Convert Binary to Hexadecimal

PHP bin2hex() Function The bin2hex() function in PHP converts the specified string value of ASCII characters to hexadecimal value. Syntax bin2hex(str) Parameter str(Required): This parameter represents the string to be converted into hexadecimal. Return  This function returns...

1 minute read.

PHP Example

We can create a simple program by writing the source code inside the php tag and save it with .php extension. Syntax: <?php // your code here ?> Let us take an example of PHP. <!DOCTYPE html> <html> <head>            ...

1 minute 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.

PHP crypt() Function

PHP crypt() Function The crypt () function in PHP returns a hashed string using DES, Blowfish, or MD5 algorithms. Some constants of crypt() function are as follows: [CRYPT_STD_DES][CRYPT_EXT_DES][CRYPT_MD5][CRYPT_BLOWFISH][CRYPT_SHA_256][CRYPT_SHA_512] etc. Syntax crypt ( string $str [, string $salt ] )  Parameter str(required)- This parameter represents the string to be hashed salt(optional)- This parameter...

2 minutes 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 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 strspn() Function

PHP strspn() Function The strspn() function of PHP finds the length of the initial segment of string that contains only characters from the mask parameter. Syntax strspn(string$subject,string$mask[,int$start[,int$length]] ) Parameter subject(required)- This parameter represents the input string. mask(required)- This parameter specifies the characters to find. start (optional)- It signifies the position in the string to start...

1 minute read.

PHP strcoll() Function

PHP strcoll() Function The strcoll() function in PHP compares two strings. This function is case-sensitive but not binary-safe. It uses the current locale for doing the comparisons. Syntax strcoll ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter...

1 minute read.

PHP if..Else..Elseif

PHP conditional statements are used to test the conditions. It supports different types of conditional statement. Following are the types of conditional statement if statement ..else statement ..elseif....else statement switch statement PHP if Statement PHP...

2 minutes read.

PHP Include Function

PHP Include( ) The PHP include() statement holds all the code/text that liesin the defined file and paste it into the file that uses the "include" statement. With the help of include...

3 minutes read.

PHP str_getcsv() Function

PHP str_getcsv() Function The str_getcsv() function in PHP parses a CSV string into an array and returns an array containing the fields read. Syntax str_getcsv ( string $input [, string $delimiter [, string $enclosure [, string$escape ]]] ) Parameter input(required)- This parameter represents the string to parse. delimiter(optional)- This parameter signifies...

1 minute read.

PHP array_shift() Function

PHP array_shift() Function The array_shift() in PHP shifts the first value of the array off. This function returns the same value after shortening the array by one element and moving everything. Syntax array_shift ( array &$array )  Parameter array(required)- This parameter represents the input array Return This function returns...

1 minute read.

PHP array_pad() Function

PHP array_pad() Function The array_pad() function in PHP pads the array to the given length and value and returns a copy of the array padded to size and value specified by size and value parameter. The padding takes place as...

1 minute read.

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...

1 minute read.

PHP fprintf() Function

PHP fprintf() Function The fprintf() function in PHP outputs a formatted string. This function operates as printf() but accepts an array of arguments, rather than a variable number of arguments. Syntax vprintf ( string $format , array $args ) Parameter format(required)- This parameter specifies the string and how to...

4 minutes read.

PHP parse_str() Function

PHP parse_str() Function The parse_str() function in PHP parses a query string into variables. Syntax parse_str ( string $encoded_string [, array &$result ] ) Parameter encoded_string(required)- This parameter represents the input string. result(optional)- This parameter specifies the name of an...

1 minute read.

PHP htmlentities() Function

PHP htmlentities() Function The htmlentities() function in PHP converts all characters to HTML entities. Syntax htmlentities(string,flags,character-set,double_encode);   Parameter string(required)- This parameter specifies the string to convert. flags(optional)- This parameter specifies how to handle quotes, invalid encoding and the used document type. The...

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 nl_langinfo() Function

PHP nl_langinfo() Function The nl_langinfo() function in PHP returns specific local information. This function does not work on Windows platforms. Syntax nl_langinfo ( int $item ) Parameter Item(required)- This parameter specifies the  integer value of the element or the constant name of the...

1 minute read.