AppDividend
Latest Code Tutorials

PHP array_diff Example | Array_diff() Function Tutorial

0

PHP array_diff Example | Array_diff() Function Tutorial is today’s topic. An array_diff() function compares the values of two or more arrays and returns the differences. An array_diff() function compares the values of two or more arrays, and return the array that contains the entries from an array1 that are not present in an array2 or array3, etc. The array_diff() is the inbuilt function in PHP and is used to calculate a difference between two or more arrays. This function computes difference according to the values of the items, between one or more array and return the differences in the form of the new array. The array_diff() function returns all the entries that are present in the first array which is not present in any other arrays.

PHP array_diff Example

The syntax of array_diff() is following.

array_diff(array1,array2,array3...);

The parameter array1 and array2 are required. From array3, the parameter is optional. The array1 parameter is the array to compare from, and the array2 parameter is an array to compare against.

See the following example.

<?php

// app.php

$x1 = [1, 2, 3, 4, 5];
$x2 = [19, 21, 46, 4, 2];
$finalArray = array_diff($x1, $x2);
print_r($finalArray);

In the above example, there are and 4 items which are repeating in both the arrays.

So, it will be eliminated from the final array, and remaining array items from $x1 is 1, 3, 5. So the output $finalArray contains the [1, 3, 5] values. See the following output.

 

PHP array_diff Example | Array_diff() Function Tutorial

Compare three arrays in PHP

Let’s compare the three arrays in PHP using array_diff() function.

<?php

// app.php

$x1 = [1, 2, 3, 4, 5];
$x2 = [19, 21, 46, 4, 2];
$x3 = [11, 12, 13, 14, 3];
$finalArray = array_diff($x1, $x2, $x3);
print_r($finalArray);

In this example, I have taken the third array called $x3. It has only one value 3 matching the array $x1. 

So, in the final array, item 3 is also eliminated, and we only see the two items, 1 and 5.

The output is following.

 

Compare three arrays in PHP

Two elements are considered equal if and only if (string) $elem1 === (string) $elem2, if we are comparing the string datatype. In other words, when the string representation is the same. The php array_diff function only checks one dimension of an n-dimensional array. Of course you can check for deeper dimensions by using array_diff($array1[0], $array2[0]);.

The array_diff provides a handy way of deleting array elements by their value, without having to unset it by key, through a long foreach loop and then having to rekey the array.

Compare the array of Strings in PHP

Let’s take an example of compare three arrays of Strings using array_diff() function.

<?php

// app.php

$x1 = ['krunal', 'ankit'];
$x2 = ['rushabh', 'dhaval'];
$x3 = ['niva', 'mansi', 'krunal'];
$finalArray = array_diff($x1, $x2, $x3);
print_r($finalArray);

The output is following.

 

Array_diff() Function Tutorial

Compare associative arrays

See the following example.

<?php

// app.php

$x1 = ['a' => 'krunal', 'b' => 'ankit'];
$x2 = ['c' => 'rushabh', 'd' =>'dhaval'];
$x3 = ['e' =>'niva', 'f' => 'mansi', 'g' => 'krunal'];
$finalArray = array_diff($x1, $x2, $x3);
print_r($finalArray);

The output is following.

 

Compare associative arrays

Regardless of what keys, they only check the values, and if the values are matched then it will be eliminated, and the first array’s remaining values will be included in the final array.

Finally, PHP array_diff Example | Array_diff() Function Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.