Backend Development 2 min read

PHP array_diff() – Computing the Difference Between Arrays

This article explains the PHP array_diff() function, describing its purpose, parameters, return value, and provides a complete example with code that demonstrates how it returns the elements present in the first array but absent from subsequent arrays.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP array_diff() – Computing the Difference Between Arrays

The array_diff() function compares one or more arrays and returns the values from the first array that are not present in any of the other arrays.

Parameters

array1 – The array to compare against the others.

array2 – The array to compare with array1 .

... – Optional additional arrays for comparison.

Return value

Returns an array containing all values from array1 that are not found in any of the other argument arrays, preserving the original keys.

Example

<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
?>

Output

Array
(
    [1] => blue
)
BackendProgrammingphpArrayfunctionarray_diff
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.