Home >Backend Development >PHP Problem >Delete the same elements in two arrays in php
PHP is a popular server-side development language that supports various data processing and management technologies. Among them, arrays are an important part of PHP programming and can represent various complex data structures and element collections. Suppose we have two arrays A and B, which contain several elements, and we want to implement a function: delete the same elements in A and B, that is, only keep the elements that appear in A or B but not both. Below we will introduce how to use PHP to achieve this function.
1. Array Basics
In PHP, an array is an ordered data structure that can store multiple values or key-value pairs. Each element is identified by a unique subscript. . Common operations on arrays include adding, deleting, traversing, and sorting. The following are some commonly used array functions:
• array(): Define an array variable;
• array_push(): Add one or more elements to the end of the array;
• array_pop(): Delete an element from the end of the array;
• array_shift(): Delete an element from the head of the array;
• array_unshift(): Add one or more elements to the head of the array;
• array_slice(): Extract a subcollection from an array;
• array_merge(): merge two or more arrays;
• array_splice(): delete or insert elements from an array.
2. Array deduplication
Before implementing the function of deleting the same elements, we need to consider how to remove duplicate elements in the array. PHP provides a built-in function array_unique(), which can implement deduplication operations. The syntax of this function is as follows:
array_unique(array $array [, int $sort_flags = SORT_STRING]): array
Among them, the $array parameter represents the array variable to be duplicated, and the $sort_flags parameter Indicates the sorting method. If there are multiple identical values, only the first value will be retained after deduplication, and the position of the retained value will remain unchanged. The following is an example:
$a = array(1, 2, 3, 2, 4, 3); $b = array_unique($a); // $b = [1, 2, 3, 4]
3. Delete the same elements
Now that we know how to duplicate an array, we will use an example to demonstrate how to delete the same elements. Suppose we have two arrays A and B whose elements are of the same type and have an undetermined number of duplicate elements. We want to implement a function diff() that takes two arrays as parameters and returns an array containing elements that only exist in one array, that is, removing the same elements in A and B.
Method 1: Use the array_diff() function
PHP provides the array_diff() function, which can return the difference between two or more arrays. The syntax of this function is as follows:
array_diff(array $array1, array $array2 [, array $...]): array
The array returned by this function is contained in $array1 but not All elements in $array2. If multiple arrays are given, the returned array contains elements that are not in any of the following arrays. Here is an example:
$a = array(1, 2, 3, 4, 5); $b = array(4, 5, 6, 7); $result = array_diff($a, $b); // $result = [1, 2, 3]
We can apply the result of this function to A and B to get elements that only exist in one array:
function diff($a, $b) { $unique_a = array_unique($a); $unique_b = array_unique($b); $diff_a = array_diff($unique_a, $unique_b); $diff_b = array_diff($unique_b, $unique_a); return array_merge($diff_a, $diff_b); } $a = array(1, 2, 3, 2, 4, 3); $b = array(2, 4, 5, 3); $result = diff($a, $b); // $result = [1, 5]
It can be seen that we can first Use the array_diff() function to get the difference between two arrays, and then merge the differences together.
Method 2: Use foreach loop
In addition to using built-in functions, we can also achieve the function of deleting the same elements by traversing two arrays. Specifically, we can first deduplicate two arrays, then iterate over one of the arrays, and for each element in it, if the element is not in the other array, add the element to the resulting array. The following is an example:
function diff($a, $b) { $unique_a = array_unique($a); $unique_b = array_unique($b); $result = array(); foreach ($unique_a as $x) { if (!in_array($x, $unique_b)) { $result[] = $x; } } foreach ($unique_b as $y) { if (!in_array($y, $unique_a)) { $result[] = $y; } } return $result; } $a = array(1, 2, 3, 2, 4, 3); $b = array(2, 4, 5, 3); $result = diff($a, $b); // $result = [1, 5]
It can be seen that we can use a foreach loop to traverse the array, and for each element, use the in_array() function to check whether the element exists in another array, if not, then added to the result array.
4. Summary
PHP is a powerful programming language, of which arrays are an important component. This article introduces how to use PHP to delete the same elements of two arrays, including using built-in functions and traversing arrays. We can choose different methods according to actual needs to achieve the best results. I hope this article will help readers understand the basic usage of PHP arrays.
The above is the detailed content of Delete the same elements in two arrays in php. For more information, please follow other related articles on the PHP Chinese website!