PHP

Find Common Values Across Multiple PHP Arrays

Efficiently identify and extract all values that are present in every one of several PHP arrays using the `array_intersect` function, useful for data validation or finding common elements.

$array1 = [1, 2, 3, 4, 5];
$array2 = [3, 4, 5, 6, 7];
$array3 = [4, 5, 8, 9, 10];

// Find values common to all three arrays
$commonValues = array_intersect($array1, $array2, $array3);

print_r($commonValues);
// Output: Array ( [0] => 4 [1] => 5 )
How it works: The `array_intersect()` function compares the values in one array against the values in one or more other arrays and returns an array containing all values that are present in all the arrays. It's a powerful tool for finding shared elements without needing to loop manually, improving code readability and performance for intersection operations across multiple datasets.

Need help integrating this into your project?

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

Hire DigitalCodeLabs