Example 1 — Finding Common Values
Keep only the values of the first list that also exist in the second list.
<?php
$a = [1, 2, 3, 4, 5];
$b = [2, 4, 6];
print_r(array_intersect($a, $b)); How It Works
- array_intersect() checks each value of $a against $b.
- The values 2 and 4 exist in both arrays, so they are kept.
- The values 1, 3 and 5 are not in $b, so they are removed.
- The kept values keep their original keys 1 and 3.

