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.
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
)Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.