0
0
PhpProgramBeginner · 2 min read

PHP Program to Find Common Elements in Arrays

Use the PHP function array_intersect($array1, $array2) to find common elements between two arrays; it returns an array of values present in both.
📋

Examples

Inputarray1 = [1, 2, 3], array2 = [2, 3, 4]
Output[2, 3]
Inputarray1 = ['apple', 'banana'], array2 = ['banana', 'cherry']
Output['banana']
Inputarray1 = [5, 6], array2 = [7, 8]
Output[]
🧠

How to Think About It

To find common elements, compare each item in the first array with items in the second array and collect those that appear in both. This is like finding friends you have in common by checking your friend list against theirs.
📐

Algorithm

1
Get the first array and the second array as input.
2
Compare elements of the first array with the second array.
3
Collect elements that exist in both arrays.
4
Return the collection of common elements.
💻

Code

php
<?php
$array1 = [1, 2, 3, 4];
$array2 = [3, 4, 5, 6];
$common = array_intersect($array1, $array2);
print_r($common);
?>
Output
Array ( [2] => 3 [3] => 4 )
🔍

Dry Run

Let's trace the example arrays [1, 2, 3, 4] and [3, 4, 5, 6] through the code.

1

Input arrays

$array1 = [1, 2, 3, 4], $array2 = [3, 4, 5, 6]

2

Find common elements

array_intersect compares elements and finds 3 and 4 in both arrays

3

Output result

Returns array with keys 2 => 3 and 3 => 4

StepCommon Elements Found
Compare 1No
Compare 2No
Compare 3Yes (3)
Compare 4Yes (4)
💡

Why This Works

Step 1: Using array_intersect

The array_intersect function compares values of two arrays and returns those that appear in both.

Step 2: Preserves keys

It keeps the keys from the first array for the common elements, which helps identify their original positions.

Step 3: Simple and efficient

This built-in function is optimized and easy to use for finding common elements without manual loops.

🔄

Alternative Approaches

Manual loop comparison
php
<?php
$array1 = [1, 2, 3, 4];
$array2 = [3, 4, 5, 6];
$common = [];
foreach ($array1 as $value) {
    if (in_array($value, $array2)) {
        $common[] = $value;
    }
}
print_r($common);
?>
This method uses loops and checks each element manually; it is less efficient but good for learning.
Using array_filter and in_array
php
<?php
$array1 = [1, 2, 3, 4];
$array2 = [3, 4, 5, 6];
$common = array_filter($array1, fn($v) => in_array($v, $array2));
print_r($common);
?>
This uses functional programming style with array_filter and is concise but slightly less readable for beginners.

Complexity: O(n*m) time, O(min(n, m)) space

Time Complexity

The function compares each element of the first array with elements of the second, leading to O(n*m) in worst case where n and m are array sizes.

Space Complexity

It creates a new array for common elements, which at most is the size of the smaller array, so O(min(n, m)).

Which Approach is Fastest?

Using array_intersect is generally faster and more optimized than manual loops or array_filter for large arrays.

ApproachTimeSpaceBest For
array_intersectO(n*m)O(min(n,m))Quick, built-in, optimized
Manual loop with in_arrayO(n*m)O(min(n,m))Learning and control
array_filter with in_arrayO(n*m)O(min(n,m))Functional style, concise code
💡
Use array_intersect for a quick and clean way to find common elements between arrays.
⚠️
Beginners often forget that array_intersect preserves keys from the first array, which can confuse output if not expected.