The array_intersect_assoc() is a builtin function in PHP and is used to compute the intersection of two or more arrays. This function is similar to the function array_intersect() which is discussed in the article PHP | array_intersect() function. The function is also used to compare the values of two or more arrays and returns the matches. The only difference is that the function returns all of the values of the first array that are present in all other arguments at the same index as that in first array, i.e., the keys are mainly used in the comparison.

**Syntax:**

arrayarray_intersect_assoc($array1, $array2, $array3,...)

**Parameters:** The array_intersect_assoc() function takes atleast two arrays as parameter. The function can take any number of arrays as arguments greater than or equal to two.

**Return Value:** The function returns another array that contains the intersection of all the input arrays. If no element matches then, a NULL array is returned.

Examples:

Input : $array1 = ("1" => "shyam", "2" => "rishav", "3" => "gaurav"); $array2 = ("1" => "shyam", "2" => "rishi", "3" => "rishav"); $array3 = ("1" => "shyam", "2" => "rishav", "3" => "ravi"); Output : Array ( [1] => shyam )

In the below program, we have used array_intersect_assoc() to find the intersection between arrays. Let’s look closer at the outputs of this and array_intersect() function.

`<?php ` ` ` `// PHP function to illustrate the use of array_intersect_assoc() ` `function` `Intersect(` `$array1` `, ` `$array2` `, ` `$array3` `) ` `{ ` ` ` `$result` `= ` `array_intersect_assoc` `(` `$array1` `, ` `$array2` `, ` `$array3` `); ` ` ` `return` `(` `$result` `); ` `} ` ` ` `$array1` `= ` `array` `(` `"1"` `=> ` `"shyam"` `, ` `"2"` `=> ` `"rishav"` `, ` `"3"` `=> ` `"gaurav"` `); ` `$array2` `= ` `array` `(` `"1"` `=> ` `"shyam"` `, ` `"2"` `=> ` `"rishi"` `, ` `"3"` `=> ` `"rishav"` `); ` `$array3` `= ` `array` `(` `"1"` `=> ` `"shyam"` `, ` `"2"` `=> ` `"rishav"` `, ` `"3"` `=> ` `"ravi"` `); ` `print_r(Intersect(` `$array1` `, ` `$array2` `, ` `$array3` `)); ` ` ` `?> ` |

Output:

Array ( [1] => shyam )

In the above program we have used the array_intersect_assoc() to find the intersection of the arrays. In the below program we will use the array_intersect() function to do the same. Give close attention to the outputs of both the programs. The first one returns only those elements which are strictly similar, both by values and keys, unlike array_intersect().

`<?php ` ` ` `// PHP function to illustrate the use of array_intersect() ` `function` `Intersect(` `$array1` `, ` `$array2` `, ` `$array3` `) ` `{ ` ` ` `$result` `= ` `array_intersect` `(` `$array1` `, ` `$array2` `, ` `$array3` `); ` ` ` `return` `(` `$result` `); ` `} ` ` ` `$array1` `= ` `array` `(` `"1"` `=> ` `"shyam"` `, ` `"2"` `=> ` `"rishav"` `, ` `"3"` `=> ` `"gaurav"` `); ` `$array2` `= ` `array` `(` `"1"` `=> ` `"shyam"` `, ` `"2"` `=> ` `"rishi"` `, ` `"3"` `=> ` `"rishav"` `); ` `$array3` `= ` `array` `(` `"1"` `=> ` `"shyam"` `, ` `"2"` `=> ` `"rishav"` `, ` `"3"` `=> ` `"ravi"` `); ` `print_r(Intersect(` `$array1` `, ` `$array2` `, ` `$array3` `)); ` ` ` `?> ` |

Output:

Array ( [1] => shyam [2] => rishav )

**Reference**: http://php.net/manual/en/function.array-intersect-assoc.php

## leave a comment

## 0 Comments