PHP

Find Differences Between Two Arrays by Value

Learn to efficiently compare two arrays and find values present in the first array but not in the second, using PHP's array_diff function.

$array1 = ['apple', 'banana', 'orange', 'grape'];
$array2 = ['banana', 'grape', 'kiwi'];

$difference = array_diff($array1, $array2);

print_r($difference);
// Output: Array
// (
//    [0] => apple
//    [2] => orange
// )
How it works: The `array_diff()` function compares `array1` against `array2` (and optionally more arrays) and returns an array containing all the values from `array1` that are not present in any of the other arrays. This is highly useful for identifying unique items or changes between two datasets, such as comparing a user's selected options against a default list.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs