How to implement array deduplication and change subscripts in PHP

PHPz
Release: 2023-04-19 09:51:55
Original
720 people have browsed it

During the development process using PHP, array deduplication is a common operation, but in many cases we not only need to deduplicate, but also need to change the subscript of the array, which requires us to process the deduplication results. This article will introduce how to use PHP to deduplicate arrays and change subscripts.

1. Use array_unique() function to deduplicate arrays

In PHP, we can use array_unique() function to deduplicate arrays. This function will remove duplicate values ​​in the array. and returns a new array. For example:

$arr = array('apple', 'banana', 'orange', 'apple', 'pear');
$new_arr = array_unique($arr);
print_r($new_arr);
Copy after login

The output result is:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [4] => pear
)
Copy after login
Copy after login

In the above example, the original array $arr contains two 'apple's, which are obtained after deduplication using the array_unique() function New array $new_arr, only one 'apple' is retained in the result.

2. Use foreach loop to deduplicate the array and change the subscript

Although the array_unique() function can be used to easily deduplicate the array, there is no way to control the new subscript in this way. subscript, below we will demonstrate how to use a foreach loop to deduplicate the array and change the subscript.

$arr = array('apple', 'banana', 'orange', 'apple', 'pear');
$new_arr = array();
foreach ($arr as $key=>$value) {
    if (!in_array($value, $new_arr)) {
        $new_arr[] = $value;
    }
}
print_r($new_arr);
Copy after login

Here we first define a new empty array $new_arr, and then use a foreach loop to traverse the original array $arr to determine whether each value exists in $new_arr. If it does not exist, add it to $new_arr. . The advantage of this method is that the array can be modified by manually controlling the subscripts.

If we want to retain the last duplicate value in the original array, we need to make a slight modification. We can use the array_reverse() function to flip the original array, so that the last duplicate element is retained during deduplication, for example :

$arr = array('apple', 'banana', 'orange', 'apple', 'pear', 'banana');
$new_arr = array();
$arr = array_reverse($arr);
foreach ($arr as $key=>$value) {
    if (!in_array($value, $new_arr)) {
        $new_arr[$key] = $value;
    }
}
$new_arr = array_reverse($new_arr);
print_r($new_arr);
Copy after login

In the above example, we first flip the original array, then use a foreach loop to traverse, and set the subscript of the new array to the subscript of the last occurrence of the element in the original array.

3. Use the array_flip() function to deduplicate the array and change the subscripts

Another common way to deduplicate an array is to use the array_flip() function, which can exchange the elements in the array. keys and values. If there are duplicate values ​​in the array, the subsequent keys will overwrite the previous keys, thus achieving the deduplication operation, and the new array subscript can be obtained through the array_keys() function. For example:

$arr = array('apple', 'banana', 'orange', 'apple', 'pear', 'banana');
$new_arr = array_flip($arr);
$new_arr = array_keys($new_arr);
print_r($new_arr);
Copy after login

The output result is:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [4] => pear
)
Copy after login
Copy after login

In the above example, we first use the array_flip() function to deduplicate the original array, and set the subscript of the new array to the original The value of the array, and then obtain the subscript array of the new array through the array_keys() function. This method is simple and easy to use, but it is easily disturbed by the subscript order of the new array.

4. Summary

The above is how to deduplicate arrays and change subscripts in PHP. You can choose different methods according to different actual scenarios. It should be noted that these methods all modify the original array. If you need to retain the original array, you need to use a copy to operate. At the same time, deduplication operations on arrays usually require the use of comparison functions. This is a relatively complex topic and is not explained too much in this article.

The above is the detailed content of How to implement array deduplication and change subscripts in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template