Skip to content
PHP Logo

How can PHP be used to compare arrays and identify differences in values?

To compare arrays and identify differences in values, you can use the array_diff() function in PHP. This function compares the values of two or more arrays and returns the differences as a new array. You can then use this resulting array to determine which values exist in one array but not the other.

php
$array1 = [1, 2, 3, 4, 5];
$array2 = [2, 3, 4, 5, 6];

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

print_r($differences);