array_intersect_assoc() keeps entries from the first array whose key and value both match entries in all comparison arrays.
$first = ['a' => 'red', 'b' => 'green'];
$second = ['a' => 'red', 'b' => 'blue'];
$result = array_intersect_assoc($first, $second);
print_r($result);A matching value under a different key is not considered the same entry.
$new_array = array_intersect_assoc ($array1, $array2,....);
$array1 : Required , the input array which will be checked. $first=array('One' =>1,'Two'=>2,'Three'=>3,'Four'=>'Fourth');
$second=array('One'=>1,'Two'=>2,'Third'=>3);
$result1=array_intersect_assoc($first,$second);
foreach ($result1 as $key => $val) {
echo "$key -> $val <br>";
}
Output is here.
One -> 1
Two -> 2
Keys of the first index are retained. ( no re-indexing done here) . The third element is not included as the key is different in both arrays.
$first=array('One' =>1,'Two'=>2,'Three'=>3,'Four'=>'Fourth');
$second=array('One'=>1,'Two'=>2,'Third'=>3);
$third=array('One'=>1);
$result1=array_intersect_assoc($first,$second,$third);
foreach ($result1 as $key => $val) {
echo "$key -> $val <br>";
}
Output is here.
One -> 1
$array1 = ["A" => "apple", "b" => "banana"];
$array2 = ["a" => "apple", "B" => "banana"];
$result = array_intersect_uassoc($array1, $array2, 'strcasecmp');
print_r($result);
// Output: Array ( [A] => apple [b] => banana )
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.