PHP compare two arrays and get matched values not difference

In this post we will show you PHP compare two arrays and get matched values not difference, hear for PHP compare two arrays and get matched values not difference we will give you detail for implement.

SOULTION FOR PHP COMPARE TWO ARRAYS AND GET MATCHED VALUES NOT DIFFERENCE

$array1 = array(
	"America",
	"Armenia",
	"Belarus",
	"Cambodia",
	"Denmark",
	"Ethiopia",
	"France",
	"Ghana"
);

$array2 = array(
	"America",
	"Armenia",
	"Belarus",
	"Cambodia",
	"Denmark",
	"Ethiopia",
	"France",
	"Ghana",
	"Russia",
	"Slovakia"
);

$output = array_intersect($array1, $array2);
// Expected Output:
// array("Russia", "Slovakia");
$output = array_intersect($array1, $array2);

if you trying to compare two arrays and get only the values that exist on both arrays but, unfortunately, you can’t find the right array with array_diff()

for solution use can array_intersect() instead of array_diff()

PHP array_intersect() Function

Compare the values of two arrays, and return the matches.

Definition and Usage

The array_intersect() function compares the values of two (or more) arrays, and returns the matches.

This function compares the values of two or more arrays, and return an array that contains the entries from array1 that are present in array2, array3, array4 etc.

$array1 = array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$array2 = array("a"=>"red","b"=>"black","h"=>"yellow");
$array3 = array("e"=>"red","f"=>"black","g"=>"purple");

$output = array_intersect($array1,$array2,$array3);
print_r($output);

Hope this code and post will helped you for implement PHP compare two arrays and get matched values not difference. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs