array_intersect_assoc(): Intersection of two or more arrays

This function takes two or more arrays as input and then returns the associative array containing all the values in input array ( $array1) that are present in all of the arguments.
$new_array = array_intersect_assoc ($array1, $array2,....);
$array1 : Required , the input array which will be checked.
$array2 : Required , the value and key to be matched with $array1.

Here keys are also used for comparison in addition to values. ( In array_intersect() only values are used for comparison )

Example with two arrays

$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
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.

Example with three 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);
while (list ($key, $val) = each ($result1)) {
echo "$key -> $val <br>";
}
Output is here.
One -> 1

Example: Case-Insensitive Key Comparison

$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 )

Difference between arrays considering values
Array REFERENCE
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate 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.



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer