Home > Backend Development > PHP Tutorial > How to maintain key-value correspondence after deduplication in PHP array?

How to maintain key-value correspondence after deduplication in PHP array?

王林
Release: 2024-04-27 12:33:02
Original
683 people have browsed it

PHP Methods to maintain key-value correspondence after deduplication in an array include: using the array_unique() function to remove duplicate values, and then using the array_flip() function to exchange key-value pairs. Merge the original array with the deduplicated array, and use the array merging method to retain key-value correspondence.

PHP 数组去重后如何保持键值对应?

PHP array maintains key value correspondence after deduplication

Introduction

In When dealing with PHP arrays, sometimes we need to remove duplicate elements while maintaining their key-value correspondence. This tutorial will show you how to use the array_unique() function in combination with other methods to achieve this goal.

array_unique() function

array_unique() function can be used to remove duplicate values ​​from an array. By default, it compares the values ​​in the array and retains the key-value correspondence of the first occurrence.

Code example

$array = ['foo', 'bar', 'baz', 'foo', 'xyz'];

$uniqueArray = array_unique($array);

print_r($uniqueArray);
Copy after login

Output:

Array
(
    [0] => foo
    [1] => bar
    [2] => baz
    [4] => xyz
)
Copy after login

Keep key value corresponding

In order to keep the array For key-value mapping of elements, we need to use additional steps. One way is to use the array_flip() function, which swaps the key and value of a key-value pair.

Code example

$flippedArray = array_flip($uniqueArray);

print_r($flippedArray);
Copy after login

Output:

Array
(
    [foo] => 0
    [bar] => 1
    [baz] => 2
    [xyz] => 4
)
Copy after login

Now, we have a unique array that keeps key-value correspondence.

Using merge

Another way to keep key-value correspondence is to use array merge. This method creates a new associative array using the original array and the deduplicated array.

Code Example

$newArray = array_merge($array, $uniqueArray);

print_r($newArray);
Copy after login

Output:

Array
(
    [0] => foo
    [3] => foo
    [1] => bar
    [4] => baz
    [2] => xyz
)
Copy after login

This method does not filter duplicate values, but it retains the key-value correspondence of the original array.

The above is the detailed content of How to maintain key-value correspondence after deduplication in PHP array?. 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