How to compare two array values in PHP

Topic: PHP / MySQL« Prev|Next »

Answer: Use the PHP array_diff() function

You can use the PHP array_diff() function to compare an array against one or more other arrays. The array_diff() function returns the values in the first array that are not present in any of the other arrays. Here's an example.

  • <?php
  • $array1 = array("a" => "sky", "star", "moon", "cloud", "moon");
  • $array2 = array("b" => "sky", "sun", "moon");
  •  
  • // Comparing the values
  • $result = array_diff($array1, $array2);
  • print_r($result);
  • ?>

Related FAQ

Here are some more FAQ related to this topic:

 
Close

Your Feedback:

 

We would love to hear from you! Please say something.