php compare two arrays for matches

PHP compare two arrays and get matched values not difference

PHP compare two arrays and get matched values not difference

In this PHP compare two arrays and get matched values not difference 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()

READ :  Last Inserted Id Using Laravel Eloquent Examples

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.

Leave a Reply

Your email address will not be published. Required fields are marked *