Home >Backend Development >PHP Problem >How to delete the intersection of two arrays in php

How to delete the intersection of two arrays in php

PHPz
PHPzOriginal
2023-04-19 11:35:55629browse

In PHP programming, it is very common to encounter the problem of deleting the intersection of two arrays, especially when the intersection of multiple arrays needs to be processed. This article will introduce you to a simple and practical method to delete the intersection between two arrays.

1. Problem Description

Suppose there are two arrays $A and $B, respectively as follows:

$A = array(1,2,3,4,5 ,6);

$B = array(4,5,7,8,9,10);

The requirement is to retain only all elements of $A and $B, but not two The intersection part in the arrays, that is, the result array should be:

$Result = array(1,2,3,6,7,8,9,10);

So what should I do? What about implementation? The following will introduce you one by one.

2. Method 1

First of all, we can use the array_diff function in PHP to implement the difference operation of array elements. The code is as follows:

$Result = array_merge(array_diff ($A, $B), array_diff($B, $A));

In the above code, array_diff($A, $B) means filtering out items in the $A array but not in $B Elements in the array; array_diff($B, $A) means to filter out elements that are in the $B array but not in the $A array. Then use the array_merge function to combine the two result arrays. This method seems simple, but in fact it is not robust, as shown in the following points:

  1. cannot handle the situation where there are multiple identical elements in the array. Because when merging two arrays, the same elements have actually been merged, so duplication cannot be removed.
  2. Cannot handle inconsistent element types. For example, there is a string type element 'hello' in $A but not in $B, but we hope that the final output Result array is the union of all elements, then an error will be reported when using the array_merge function.

So, in order to better avoid these problems, we need to use a more robust method to delete the intersection of array elements.

3. Method 2

Next, we can loop through each element in the array, and perform deduplication and filtering operations in the process. The code is as follows:

$Result = array();

foreach($A as $value){

if(!in_array($value, $B)){
    $Result[] = $value;
}

}

foreach($B as $value){

if(!in_array($value, $Result)){
    $Result[] = $value;
}

}

In the above code, we first define an empty array $Result, representing the final result array. Then use two foreach loops to iterate through each element in $A and $B. During the process, use the in_array function to determine whether the current element appears in another array, such as whether it appears in $B. If it does not appear before, it is added to $Result. The final $Result array is the union of all elements in the two arrays.

Using this method has the following advantages:

  1. It can handle the situation where there are multiple identical elements in the array, because elements are added to the new array one by one.
  2. Can handle inconsistent element types because the values ​​of elements are compared one by one through the in_array function.
  3. When the array size is small, the efficiency is higher.

4. Summary

In PHP, this article introduces two ways to deal with array intersection problems: using the array_diff function and loop traversal filtering. Although both methods can get correct results, the method of using loop traversal is more robust and can handle more edge cases, which is worth learning.

The above is the detailed content of How to delete the intersection of two arrays in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn