Last Updated on August 28, 2021 by Roshan Parihar
PHP array_diff() Function is used to compare only the values of two or more arrays. After the comparison. After comparison, it returns the difference of the first array with other arrays without considering the keys.
It compares the values of the first array (array1) with the values of the rest of the arrays in PHP. After that, it returns the values of the first array (array1) that are not present in the rest of the arrays.
Let’s find out the use of the PHP array_diff()
function with the examples given below.
Syntax of PHP array_diff() Function
The array_diff()
function takes comma-separated arrays as an argument.
Description of Parameters
Name | Description |
---|---|
array1 | Required. It is the first array to compare the values with other arrays. |
array2 | Required. Specify the second array to compare against. |
array3…arrayN | optional. Specify more arrays to compare against. |
Let’s find out the use of the PHP array_diff()
Function with the examples given below.
Examples of PHP array_diff() Function
See the examples given below to learn comparing the values of the arrays using the array_diff()
Function in PHP.
Example 1: Compare the Difference of Values of Two Arrays in PHP
Suppose that, if you have two arrays to compare the difference of values, the PHP array_diff()
function compares the values of the first array with the values of the second array. After the comparison, it returns the difference of values of the first array that are not present in the second array without considering the keys.
1 2 3 4 5 6 7 8 9 10 11 |
<?php //Declare an associative array in PHP $myarrayassoc1 = array("Cycle" => 2, "Bike" => 5, "Car" => 9, "Ricksaw" => 4); $myarrayassoc2 = array("Cycle" => 2, "Car" => 4, "Bolero" => 9, "WagonR" => 4); //Compare the differrence of values in array $newArr = array_diff($myarrayassoc1, $myarrayassoc2); //Print result print_r($newArr); ?> |
Output
The above output shows that the value ‘5’ is not matching with the values of the second array regardless of keys. That’s why it prints that value in the output with its key.
Example 2: Get the Difference of Values from More than Two Arrays in PHP
In addition to the above example, you can also get the difference of values for more than two arrays using the PHP array_diff()
function. It first compares their values to find the difference. After that, it returns the difference of the values of the first array that are not present in the second and third arrays.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php //Declare an associative array in PHP $myarrayassoc1 = array("Cycle" => 2, "Bike" => 5, "Car" => 9, "Ricksaw" => 4, "WagonR" => 7); $myarrayassoc2 = array("Cycle" => 2, "WagonR" => 5, "Bolero" => 8, "Ricksaw" => 4); $myarrayassoc3 = array("Cycle" => 2, "Bike" => 5, "Bolero" => 8, "Car" => 4); //Get the differrence among more that two arrays $newArr = array_diff($myarrayassoc1, $myarrayassoc2, $myarrayassoc3); //Print result print_r($newArr); ?> |
Output
The above example contains the two elements whose values are different and not present in the second and third arrays in PHP.
You May Also Like to Read