Current Location: Home> Latest Articles> How to Use the array_intersect Function to Determine If Two Arrays Have Common Elements? Code Examples and Detailed Explanation

How to Use the array_intersect Function to Determine If Two Arrays Have Common Elements? Code Examples and Detailed Explanation

gitbox 2025-09-01
<span class="hljs-meta"><?php<br>
// This section is unrelated to the article, only used to show separation<br>
echo "Article starts below...";<br>
?></p>
<p><hr></p>
<p># How to Use the array_intersect Function to Determine If Two Arrays Have Common Elements? Code Examples and Detailed Explanation<span></p>
<p>In daily PHP development, we often need to check whether two arrays contain the same elements. For example, in scenarios like permission control, tag matching, and keyword filtering, we need to quickly determine if two arrays intersect. In such cases, the <code>array_intersect
  • $array1: Required. The first array to compare.

  • $array2: Required. The second array to compare.

  • ...$arrays: Optional. Additional arrays to include in the comparison.

  • Return value: A new array containing all intersecting elements.

2. How to Check If Two Arrays Have Common Elements

The idea is simple:

  1. Use array_intersect to get the intersection of the two arrays.

  2. If the resulting array is not empty, it means there is an intersection; otherwise, there is none.

3. Code Example

Here is a simple example showing how to determine if two arrays have common elements:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$array1</span></span><span> = [</span><span><span class="hljs-number">1</span></span>, </span><span><span class="hljs-number">2</span></span>, </span><span><span class="hljs-number">3</span></span>, </span><span><span class="hljs-number">4</span></span>, </span><span><span class="hljs-number">5</span></span>];
</span><span><span class="hljs-variable">$array2</span></span><span> = [</span><span><span class="hljs-number">4</span></span>, </span><span><span class="hljs-number">5</span></span>, </span><span><span class="hljs-number">6</span></span>, </span><span><span class="hljs-number">7</span></span>, </span><span><span class="hljs-number">8</span></span>];

</span><span><span class="hljs-comment">// Get intersection using array_intersect</span></span><span>
</span><span><span class="hljs-variable">$intersection</span></span><span> = </span><span><span class="hljs-title function_ invoke__">array_intersect</span></span><span>(</span><span><span class="hljs-variable">$array1</span></span>, </span><span><span class="hljs-variable">$array2</span></span>);

</span><span><span class="hljs-keyword">if</span></span><span> (!</span><span><span class="hljs-keyword">empty</span></span><span>(</span><span><span class="hljs-variable">$intersection</span></span><span>)) {
    </span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"The two arrays have common elements: "</span></span><span>;
    </span><span><span class="hljs-title function_ invoke__">print_r</span></span><span>(</span><span><span class="hljs-variable">$intersection</span></span><span>);
} </span><span><span class="hljs-keyword">else</span></span><span> {
    </span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"The two arrays do not intersect"</span></span><span>;
}
</span></span>

Output:

<span><span>The two arrays have common elements: </span><span><span class="hljs-title class_">Array</span></span><span> ( [</span><span><span class="hljs-number">3</span></span><span>] => </span><span><span class="hljs-number">4</span></span> [</span><span><span class="hljs-number">4</span></span><span>] => </span><span><span class="hljs-number">5</span></span> )
</span></span>

As shown, the intersection of $array1 and $array2 is [4, 5], so the returned result is not empty.

4. Further Optimization for Checking Intersection

In some cases, we only care about whether an intersection exists, not the specific elements. The logic can be simplified:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$array1</span></span><span> = [</span><span><span class="hljs-string">'apple'</span></span>, </span><span><span class="hljs-string">'banana'</span></span>, </span><span><span class="hljs-string">'orange'</span></span>];
</span><span><span class="hljs-variable">$array2</span></span><span> = [</span><span><span class="hljs-string">'grape'</span></span>, </span><span><span class="hljs-string">'peach'</span></span>, </span><span><span class="hljs-string">'banana'</span></span>];

</span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-title function_ invoke__">array_intersect</span></span><span>(</span><span><span class="hljs-variable">$array1</span></span>, </span><span><span class="hljs-variable">$array2</span></span><span>)) {
    </span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Intersection exists"</span></span><span>;
} </span><span><span class="hljs-keyword">else</span></span><span> {
    </span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"No intersection"</span></span><span>;
}
</span></span>

This makes it easier to directly see whether an intersection exists.

5. Example Use Cases

  1. Permission Check
    Take the intersection of a user role array and a system permission array. If not empty, the user has access permission.

  2. Tag Matching
    Compare user interest tags with content tags. If an intersection exists, recommend related content.

  3. Keyword Filtering
    Compare a user-input keyword array with a list of sensitive words. If there is an intersection, alert or block the input.

6. Conclusion

  • array_intersect can efficiently find intersections of multiple arrays.

  • By checking if the intersection is empty, we can determine whether two arrays share any elements.

  • It is widely used in real-world applications like permission control, recommendation systems, and content filtering.

By effectively using array_intersect, we can implement powerful logic with minimal code, greatly improving development efficiency.

<span></span>