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
- Get, Write, Read, Load, JSON File from Url PHP
- Functions: Remove First Character From String PHP
- Remove Specific/Special Characters From String In PHP
- How to Replace First and Last Character From String PHP
- Reverse String in PHP
- Array Push, POP PHP | PHP Array Tutorial
- PHP Search Multidimensional Array By key, value and return key
- json_encode()- Convert Array To JSON | Object To JSON PHP
- PHP remove duplicates from multidimensional array
- PHP Remove Duplicate Elements or Values from Array PHP