array_intersect

array_intersect(array $array, array ...$arrays): array

Computes the array intersection: values present in every array, ordered by the first array.

C C23

C23
#include <stdio.h>

int main(void) {
    int xs[] = {1, 2, 3, 4, 5};
    int keep[] = {3, 4, 9};
    size_t n = sizeof xs / sizeof xs[0];
    size_t m = sizeof keep / sizeof keep[0];
    printf("[");
    int first = 1;
    for (size_t i = 0; i < n; i++) { // hand-written intersection: membership scan
        for (size_t j = 0; j < m; j++) {
            if (xs[i] == keep[j]) {
                printf(first ? "%d" : ", %d", xs[i]);
                first = 0;
                break;
            }
        }
    }
    printf("]\n");
    return 0;
}
Output
[3, 4]

C++ C++26

C++26
#include <iostream>
#include <vector>

int main() {
    std::vector<int> xs = {1, 2, 3, 4, 5};
    std::vector<int> keep = {3, 4, 9};
    std::vector<int> out;
    for (int x : xs) {
        bool found = false;
        for (int k : keep) {
            if (x == k) { found = true; break; }
        }
        if (found) out.push_back(x);
    }
    std::cout << "[";
    for (size_t i = 0; i < out.size(); ++i) {
        if (i) std::cout << ", ";
        std::cout << out[i];
    }
    std::cout << "]\n";
    return 0;
}
Output
[3, 4]

C# v11.0.100

v11.0.100
var xs = new List<int> { 1, 2, 3, 4, 5 };
var common = xs.Intersect(new[] { 3, 4, 9 }).ToList(); // LINQ set intersection (also dedupes)
Console.WriteLine($"[{string.Join(", ", common)}]");
Output
[3, 4]

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
    a := []int{1, 2, 3, 4, 5}
    keep := map[int]bool{3: true, 4: true, 9: true}
    var out []int
    for _, v := range a {
        if keep[v] {
            out = append(out, v)
        }
    }
    fmt.Println(out)
}
Output
[3 4]

Java v27.0.1

v27.0.1
import java.util.Arrays;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Set<Integer> keep = Set.of(3, 4, 9);
        int[] out = Arrays.stream(new int[]{1, 2, 3, 4, 5})
                .filter(keep::contains)
                .toArray();
        System.out.println(Arrays.toString(out));
    }
}
Output
[3, 4]

JavaScript v26.13.1

v26.13.1
const keep = new Set([3, 4, 9]);
console.log([1, 2, 3, 4, 5].filter((x) => keep.has(x)));
Output
[ 3, 4 ]

PHP v8.6.0

v8.6.0
// array_intersect keeps keys, so renumber with array_values for a clean JSON list
echo json_encode(array_values(array_intersect([1, 2, 3, 4, 5], [3, 4, 9])));
Output
[3,4]

Python v3.15.2

v3.15.2
print([x for x in [1, 2, 3, 4, 5] if x in {3, 4, 9}])  # order preserved, duplicates kept
Output
[3, 4]

Rust v1.99.1

v1.99.1
fn main() {
    let xs = [1, 2, 3, 4, 5];
    let keep = [3, 4, 9];
    let out: Vec<i32> = xs.iter().filter(|x| keep.contains(x)).copied().collect();
    println!("{:?}", out);
}
Output
[3, 4]

TypeScript v26.13.1

v26.13.1
const keep: ReadonlySet<number> = new Set([3, 4, 9]);
console.log([1, 2, 3, 4, 5].filter((x) => keep.has(x)));
Output
[ 3, 4 ]

Language differences

C: hand-writes the nested scan; building the second collection as a Set/map drops the O(n×m) scan to O(n+m).

C++: hand-writes the nested scan.

C#: Intersect is a true set intersection (deduped, ordered by first appearance in the first sequence).

Go: filters along the first sequence (order-preserving, duplicates kept).

Java: filters along the first sequence (order-preserving, duplicates kept).

JavaScript: filters along the first sequence (order-preserving, duplicates kept).

PHP: array_intersect also compares loosely as strings, accepts multiple arrays, and keeps the first array's keys and order (with the usual array_values wrapper in the example); variants: array_intersect_key by keys, array_intersect_assoc by key+value; with 3+ arrays the semantics mean "present in all arrays", and extending the hand-written versions to multiple arrays means checking each membership container in turn.

Python: as with the difference entry, the semantic split is between "filtering" and "set operations" — the comprehension filters along the first sequence (order-preserving, duplicates kept); the set intersection {3,4,9} & set(a) is unordered entirely.

Rust: filters along the first sequence (order-preserving, duplicates kept).