php

PHP: Compare two arrays and get the difference

In this tutorial, we are going to see how to compare two arrays and get the difference. You can use array_diff() function to compare an array to one or more other arrays. array_diff() function returns values from the first array that are not present in any of the other arrays.
 

How to compare two arrays and get the difference
<?php
	$arr1 = array("a", "b", "f", "g");
	$arr2 = array("a", "b", "c", "d", "e");
	 
	// Compare values
	$cmp = array_diff($arr1, $arr2);
	print_r($cmp);
?>

Output:

Array ( 
	[2] => f 
	[3] => g 
)

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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