Home  >  Article  >  Backend Development  >  How to remove identical elements from two arrays in php

How to remove identical elements from two arrays in php

PHPz
PHPzOriginal
2023-04-24 09:07:12963browse

In PHP programming, arrays are very practical data types. However, we may encounter some difficulties when we need to remove identical elements from two arrays. This article will introduce three methods to remove identical elements from two arrays.

Method 1: Use array_intersect function

The array_intersect function is a built-in function in PHP, used to obtain the intersection between two or more arrays. This function will return a new array containing the elements that are present in both arrays.

Based on this, we can remove these co-existing elements through the array_diff function. The function of array_diff function is to obtain the difference between two arrays and return a new array.

Code example:

$first_array = array('apple', 'banana', 'orange', 'peach');
$second_array = array('orange', 'pear', 'grape', 'apple');

$intersect_array = array_intersect($first_array, $second_array);
$result_array = array_diff($first_array, $intersect_array);

print_r($result_array);

Run result:

Array
(
    [1] => banana
    [3] => peach
)

Method 2: Use the array_unique function

The array_unique function is a built-in function in PHP, used to remove arrays duplicate elements in and returns a new array.

When removing the same elements from two arrays, we can merge the two arrays into a new array, and then use the array_unique function to remove duplicates. The new array after deduplication only contains elements that are different from the two original arrays.

Code example:

$first_array = array('apple', 'banana', 'orange', 'peach');
$second_array = array('orange', 'pear', 'grape', 'apple');

$merged_array = array_merge($first_array, $second_array);
$result_array = array_unique($merged_array);

print_r($result_array);

Run result:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => peach
    [5] => pear
    [6] => grape
)

Method 3: Use custom function

If we don’t want to use PHP’s built-in function, we can also Write your own function to remove identical elements from two arrays.

The implementation of this function is very simple. You only need to traverse the first array and determine whether the element appears in the second array. If it appears, remove it from the first array. Can.

Code example:

function remove_duplicate_elements($first_array, $second_array) {
    foreach ($first_array as $key => $value) {
        if (in_array($value, $second_array)) {
            unset($first_array[$key]);
        }
    }
    return $first_array;
}

$first_array = array('apple', 'banana', 'orange', 'peach');
$second_array = array('orange', 'pear', 'grape', 'apple');

$result_array = remove_duplicate_elements($first_array, $second_array);

print_r($result_array);

Run result:

Array
(
    [1] => banana
    [3] => peach
)

Summary

In PHP programming, removing the same elements from two arrays is a Common operations. This article introduces three methods to achieve this goal, namely using the array_intersect and array_diff functions, using the array_unique function, and custom functions. We can choose different methods according to actual needs. The implementation of these methods is very simple, but the optimization and performance of the program need to continue to be improved.

The above is the detailed content of How to remove identical elements from 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