PHP array_intersect_assoc() function

PHP array_intersect_assoc() function

The array_intersect_assoc() function in PHP is used to compute the intersection of arrays with the help of an additional index check. It returns an array containing all the entries from array1 that are present in all the other arrays.

Syntax

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

Parameter

array1(required)- This parameter signifies the input array to compare from.

array2(required)- This parameter signifies the input array to compare against.

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

Return

This function returns an array containing all the values in array1 that are present in all the other arrays.

Example 1

 

Output

Array
 (
     [0] => A
 ) 

Example 2

 

Output

Array 1: 
 array (
   0 => 'aqua',
   1 => 'green',
   2 => 'red',
   3 => 'blue',
   4 => 'red',
 ) 
 Array 2: 
 array (
   0 => 'blue',
   1 => 'green',
   2 => 'yellow',
   3 => 'blue', 
 )
  Intersection: 
 Array
 (
     [1] => green
     [3] => blue 
 ) 

Example 3

 "Reema", 2 => "Akash", 3 => "Sukla", 4 =>"Varun",5=>"Mukta",6=>"Amar");
 //initializing array2
 $array2 = array(1 => "Reema", 2 => "Akash", 3 => "Tisha", 4 =>"Varun");
 //initializing array3
 $array3 = array(1 => "Rahul", 2 => "Akash", 3 => "Tisha", 4 =>"Varun"); 
 //computing the intersection between array1, array2 and array3
 $intersection = array_diff($array1,$array2,$array3);
 //printing the intersection containing all the entries from array1 that are present in array2 and array3
 print_r($intersection);
 ?> 

Output

Array
 (
     [3] => Sukla
     [5] => Mukta
     [6] => Amar
 ) 

Related Topics

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

PHP crc32() Function The crc32() function in PHP  Calculates the crc32 polynomial of a string. Syntax crc32( string $str ) Parameter str(required)- This parameter represents The string to be calculated Return This function returns the crc32 checksum of string as an integer. Example 1 ...

1 minute 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 is_double() Function

The PHP is_double() 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 substr_count() Function

PHP substr_count() Function The substr_count() function in PHP counts the number of times a substring occurs in the specified string. Syntax substr_count ( string $haystack , string $needle [, int $offset  [, int $length ]] ) Parameter haystack(required)- This parameter signifies the string to search in. needle (required)- This parameter represents the substring to search...

1 minute 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 array_reverse() Function

PHP array_reverse() Function The array_reverse() function in PHP returns a new array with the elements in the reversed order. Syntax array_reverse (array $array [, bool $preserve_keys = FALSE ] ) Parameter array(required)- This parameter represents the input array preserve_keys (optional)- This parameter specifies if the function should preserve the keys...

1 minute read.

PHP rsort() Function

PHP rsort() Function The rsort () function in PHP is used to sort an array in reverse order i.e., highest to lowest. Syntax rsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) Parameter array(required)- This parameter represents the input array. sort_flags (optional)- This parameter is used to...

1 minute read.

PHP chunk_split() Function

PHP chunk_split() Function The chunk_split() function in PHP splits a string into a series of smaller parts. Syntax chunk_split(string,length,end) Parameter string(required)- It specifies the string to split. length(optional)- This parameter represents a number that defines the...

1 minute read.

PHP strcmp() Function

PHP strcmp() Function The strcmp() function in PHP compares two strings. It is a binary-safe case-sensitive string function. This function is similar to the strncmp() function, with the difference that you can specify the number of characters from each...

1 minute read.

PHP array_sum() Function

PHP array_sum() Function The array_sum() function in PHP is used to calculate the sum of values in an array. Syntax array_sum ( array $array ) Parameter array(required)- This array represents the input array. Return This function returns the sum of values as an...

1 minute read.

PHP quotemeta() Function

PHP quotemeta() Function The quotemeta() function in PHP quotes the meta characters and returns a string with a backslash character (\) before some predefine characters. The predefined characters are as follows: period ...

1 minute read.

PHP strlen() Function

PHP strlen() Function The strlen() function in PHP is used to return the length of the specified string. Syntax strlen ( string $string ) Parameter string(required)- This parameter represents the string which is measured for length. Return This function returns the length of the string on success or returns 0 if the specified...

1 minute read.

PHP str_rot13() Function

PHP str_rot13() Function The str_rot13() function in PHP performs the ROT13 encoding on a string and returns the resulting string. The ROT13 encoding is used to shift every letter by 13 places in the alphabet...

1 minute read.

How to use Date and Time in PHP

HOW TO USE DATE AND TIME IN PHP: PHP time and date function  informs us about current date and time. it can be manipulated also. How time() function works: The time() function...

2 minutes read.

PHP array_intersect() function

The array_intersect () function in PHP is used to compute the intersection of arrays. It returns an array containing all the entries from array1 that are present in all the other arrays. Syntax array_intersect ( array $array1 , array $array2 [, array $... ]) Parameter array1(required)- This parameter...

1 minute read.

PHP array_uintersect_assoc() Function

PHP array_uintersect_assoc() Function The array_uintersect_uassoc() function in PHP calculates the intersection of arrays by comparing the data and indexes by separate callback functions. It returns an array containing all the values of the first input...

2 minutes read.

PHP htmlspecialchars_decode() Function

PHP htmlspecialchars_decode() Function The htmlspecialchars_decode() function in PHP convert some predefined HTML entities to characters. The some predefined HTML entities that will be decoded are as follows: & OR & (ampersand)" OR " (double quote)'...

2 minutes read.

PHP Do While Loop

PHP do while loop is used to execute the code at least one time because condition is checked after executing the code. Syntax: do{ //code to be executed  }while(condition); Output <?php $n=1;  do{ echo "$n<br/>";  $n++;  }while($n<=5);  ?> Output 1 2 3 4 5 ← Prev Next →...

1 minute read.

PHP is_array() Function

The is_array() Function in PHP is used to  find whether a variable is an array or not. It returns a Boolean value true if the parameter var is an array else it returns...

1 minute read.