How to Compare Two or More Array Values in PHP

How to Compare Two or More Array Values in PHP

In PHP, you may need to compare the values of two or more arrays to determine if they have any common elements or if their values are identical. In this article, you will learn different ways to compare the values of two or more arrays using PHP.

How to Compare Two or More Array Values in PHP

In PHP, comparing the values of two or more arrays can be a common requirement in various scenarios. It can be used to check if two arrays have the same values, or to find the differences between multiple arrays. PHP provides several built-in functions that can be used to compare array values. you will explore some of these functions and provide examples of how to use them; is as follows:

  • 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
)

This is because “banana” and “pear” are the only values that are present in both $array1 and $array2. The [1] and [3] keys indicate the positions of these values in the resulting array.

Comparing Array Values using array_diff() Function

PHP array_diff() function compares the values of two or more arrays values. And returns a new array with unique values. It returns an array that contains all the values that are 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);

This code is an example of how to find the unique values in one array compared to another array using the array_diff() function in PHP.

The first line creates an array $array1 with the values “apple”, “banana”, “orange”, and “pear”. The second line creates an array $array2 with the values “banana”, “pear”, “grape”, and “watermelon”.

The array_diff() function is then used to compare the two arrays $array1 and $array2 and returns an array containing the values that are present in $array1 but not in $array2. This resulting array is assigned to the variable $unique_values.

Finally, the print_r() function is used to display the values of the $unique_values array.

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 given code is creating two associative arrays $array1 and $array2 with keys ‘a’, ‘b’, ‘c’, and ‘d’ and their respective values ‘apple’, ‘banana’, ‘orange’, ‘pear’ and ‘banana’, ‘pear’, ‘grape’, ‘watermelon’.

Then, the array_intersect_assoc() function is used to compare both arrays and find the common values between them, while taking into account the keys of both arrays. In other words, it will only return the elements of $array1 that have matching keys and values in $array2.

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of 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 from a starting stage. As well as demo example.

Leave a Reply

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