Compare arrays in PHP. In this tutorial, we will learn how to compare two or more arrays in PHP and create a unique array without duplicate values.
How to Compare Two or More Array Values in PHP
Array_diff() function compares the values of two or more arrays values in PHP. And returns a new array with unique values.
Before we compare arrays in PHP, You should know about the array_diff() function of PHP. Let’s see the definition, syntax, and examples of array diff() function of PHP
PHP array_diff() Function
Definition:– The PHP array_diff() function compares the values of two or more arrays values. And returns a new array with unique values.
Syntax
The syntax of array_diff() function is following:
array_diff(array_one, array_second, array_third, …, array_n);
Parameters of array_diff() function:
Parameter | Description |
---|---|
array_one | This is required. The array to compare from. |
array_second | This is required. An array to compare against. |
Example 1 – Compare two arrays in PHP
Let’s take first example, in this example, we have two arrays with duplicate values. And we will remove the duplicate values from these arrays and create a new unique array in PHP:
<?php
$array_one = array("first", "second", "third", "fourth", "five");
$array_second = array("first", "second", "third", "fourth");
$res = array_diff($array_one,$array_second);
print_r($res);
?>
The result of the above code is: Array ( [4] => five )
Example 2 – Compare Numeric Array in PHP
Let’s take the second example, in this example, we have two numeric arrays with duplicate values. And we will remove the duplicate values from these arrays and create a new unique array in PHP:
<?php
$array_one = array(1, 2, 3, 4, 5, 6);
$array_second = array(1, 2, 3, 4);
$res = array_diff($array_one,$array_second);
print_r($res);
?>
The result of the above code is: Array ( [4] => 5 [5] => 6 )
Example 3 – Compare Three Arrays in PHP
Let’s take the Third example, in this example, we have three arrays with duplicate values. And we will compare these three arrays and find the difference between PHP:
<?php
$array1 = array('a', 'b', 'c', 'd', 'e', 'f');
$array2 = array('a', 'b', 'g', 'h');
$array3 = array('a', 'f', 'i');
$res = array_diff($array1, $array2, $array3);
print_r($res);
?>
The output of the above code is: Array ( [2] => c [3] => d [4] => e )
Conclusion
Compare arrays in PHP. In this tutorial, we 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