sajad torkamani

How does it work?

array_intersect() returns an array containing all the values in the first array that are also present in all the other arrays.

The below code:

<?php

$runners = ['John', 'Bob', 'Alice'];

$finalists2020 = ['John', 'Henry', 'Bob', 'Alice'];
$finalists2021 = ['Tim', 'Jake', 'John', 'Alice'];
$finalists2022 = ['Peter', 'John', 'Alice', 'Bob'];

print_r(array_intersect($runners, $finalists2020, $finalists2021, $finalists2022));

Will output:

Array
(
    [0] => John
    [2] => Alice
)

Because only "John" and "Alice" are present in all the other arrays ($finalists2020, $finalists2021, and $finalists2022).

Tagged: PHP

Leave a comment

Your email address will not be published. Required fields are marked *