array_intersect() work is utilized to locate the coordinated components from at least two components. Capacity “array_intersect()”
thinks about the estimations of the primary cluster with different exhibits and returns coordinated components.
Syntax:
array_intersect(array1, array2, [array3,...]);
Example:
Input:
$arr1 = array("Hello", "Hi", "Okay", "Bye!");
$arr2 = array("Okay", "Hello", "Bye", "Hi");
Function calling:
array_intersect($arr1, $arr2);
Output:
Array
(
[0] => Hello
[1] => Hi
[2] => Okay
)
PHP code 1: Comparing two string arrays
<?php
//input array1
$arr1 = array("Hello", "Hi", "Okay", "Bye!");
//input array2
$arr2 = array("Okay", "Hello", "Bye", "Hi");
//finding matched elements
$ans = array_intersect($arr1, $arr2);
print_r ($ans);
?>
Output
Array
(
[0] => Hello
[1] => Hi
[2] => Okay
)
PHP code 2:Comparing three arrays with strings & numbers
<?php
//input array1
$arr1 = array(10, 20, 30, "Hello", 40, "Hi", 50);
//input array2
$arr2 = array(50, 60, "Hello", 70, 80);
//input array3
$arr3 = array(10, 20, "Hello", 50, "Hi", 30);
//finding matched elements
$ans = array_intersect($arr1, $arr2, $arr3);
print_r ($ans);
?>
Output
Array
(
[3] => Hello
[6] => 50
)