Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

array_intersect()

Keep first-array values that occur in every other array.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

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

Keep first-array values that occur in every other array. It preserves the keys and duplicates from the first array, so the result need not be a list even if the input was one. Scalar values compare by string representation. Use it to retain supported values while preserving their original positions.

Signature

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

Example output

{"1":"blue","2":"blue"}

Examples

<?php
$requested = ['red','blue','blue'];
echo json_encode(array_intersect($requested, ['blue','green']));

Result

{"1":"blue","2":"blue"}

Sources