How to Compare Two or More Array Values in PHP

In this article, you will learn different ways to compare the values of two or more arrays using PHP.

How do you compare two or more array values in PHP?

Here are some methods:

  • Comparing Array Values using array_intersect() Function
  • Comparing Array Values using array_diff() Function
  • Comparing Array Values using array_intersect_assoc() Function

Comparing Array Values using array_intersect() Function

The array_intersect() function is one of the most commonly used functions to compare the values of two or more arrays. It returns an array that contains all the values that are present in all the input arrays.

Here’s an example of how to use the array_intersect() function to compare the values of two arrays:

$array1 = array('apple', 'banana', 'orange', 'pear');
$array2 = array('banana', 'pear', 'grape', 'watermelon');

$common_values = array_intersect($array1, $array2);

print_r($common_values);

The given code is an example of how to find the common values between two arrays in PHP using the array_intersect() function.

The code starts by defining two arrays $array1 and $array2 with different values.

$array1 = array('apple', 'banana', 'orange', 'pear');
$array2 = array('banana', 'pear', 'grape', 'watermelon');

The next line of code uses the array_intersect() function to compare the values in $array1 and $array2 and returns an array containing the common values.

$common_values = array_intersect($array1, $array2);

Finally, the code uses the print_r() function to print the resulting array $common_values to the screen.

print_r($common_values);

In this example, the resulting array would be:

Array
(
[1] => banana
[3] => pear
)

Comparing Array Values using array_diff() Function

PHP array_diff() function compares the values of two or more array values. And returns a new array with unique values. It returns an array containing all the values present in the first array but not in the other arrays with values.

Here’s an example of how to use the array_diff() function to compare the values of two arrays:

$array1 = array('apple', 'banana', 'orange', 'pear');
$array2 = array('banana', 'pear', 'grape', 'watermelon');

$unique_values = array_diff($array1, $array2);

print_r($unique_values);

In this particular example, the output would be:

Array
(
    [0] => apple
    [2] => orange
)

This is because “apple” and “orange” are the only values in $array1 that are not also present in $array2.

Comparing Array Values using array_intersect_assoc() Function

The array_intersect_assoc() function is similar to the array_intersect() function, but it also checks the keys of the arrays. It returns an array containing all the values that are present in all the input arrays, and whose keys are also present in all the input arrays.

Here’s an example of how to use the array_intersect_assoc() function to compare the values of two arrays:

$array1 = array('a' => 'apple', 'b' => 'banana', 'c' => 'orange', 'd' => 'pear');
$array2 = array('a' => 'banana', 'b' => 'pear', 'c' => 'grape', 'd' => 'watermelon');

$common_values = array_intersect_assoc($array1, $array2);

print_r($common_values);

The resulting array will only contain the key-value pairs that are present in both arrays, which in this case is:

Array
(
    [b] => banana
    [d] => pear
)

Finally, the print_r() function is used to display the resulting array on the screen.

Conclusion

Compare arrays in PHP. In this tutorial, you have learned how to compare two or more arrays in PHP and create a unique array without duplicate values.

Recommended PHP Tutorials

  1. Get, Write, Read, Load, JSON File from Url PHP
  2. Functions: Remove First Character From String PHP
  3. Remove Specific/Special Characters From String In PHP
  4. How to Replace First and Last Character From String PHP
  5. Reverse String in PHP
  6. Array Push, POP PHP | PHP Array Tutorial
  7. PHP Search Multidimensional Array By key, value and return key
  8. json_encode()- Convert Array To JSON | Object To JSON PHP
  9. PHP remove duplicates from multidimensional array
  10. PHP Remove Duplicate Elements or Values from Array PHP

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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