$new_array = array_intersect ($array1, $array2,....);
$array1 : Required , the input array which will be checked. $first=array(1,2,3,4);
$second=array(1,2,3,5);
$result1=array_intersect($first,$second);
while (list ($key, $val) = each ($result1)) {
echo "$key -> $val <br>";
}
Output is here.
0 -> 1
1 -> 2
2 -> 3
Keys of the first index are retained. ( no re-indexing done here) .
$first=array('One' =>1,'Two'=>2,'Three'=>3,'Four'=>'Fourth');
$second=array('One'=>1,'Two'=>2,'Third'=>3);
$result1=array_intersect_assoc($first,$second);
while (list ($key, $val) = each ($result1)) {
echo "$key -> $val <br>";
}
Output is here.
One -> 1
Two -> 2
Three -> 3
$array1 = ["a" => "green", "b" => "red", "c" => "blue"];
$array2 = ["b" => "green", "c" => "yellow"];
$result = array_intersect($array1, $array2);
print_r($result); // Output: Array ( [a] => green )
$array1 = ["Red", "Green", "Blue"];
$array2 = ["red", "green"];
$result = array_intersect(array_map('strtolower', $array1), array_map('strtolower', $array2));
print_r($result); // Output: Array ( [0] => red [1] => green )
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.