Home > Backend Development > PHP Tutorial > Best way to deduplicate arrays and keep key names in PHP

Best way to deduplicate arrays and keep key names in PHP

PHPz
Release: 2024-05-01 13:00:02
Original
1074 people have browsed it

There are two best ways to deduplicate arrays and keep key names in PHP: array_unique(): It can deduplicate but not retain key names and re-index the array. Custom function array_unique_preserve_keys(): Use hash values ​​to compare values, which can remove duplicates and preserve key names.

PHP 中对数组去重并保持键名的最佳方法

The best way to deduplicate arrays and keep key names in PHP

In PHP, array deduplication means Remove duplicate values ​​while keeping key names unchanged. This is useful when working with data from different sources that may contain duplicate elements, such as from multiple form submissions or database queries.

Method 1: array_unique()

The array_unique() function is a built-in PHP function used to deduplicate arrays. It accepts an array as input and returns a new array in which duplicate values ​​have been removed. However, array_unique() does not preserve key names, but re-indexes the array, starting at 0.

Example:

$arr = ['a', 'b', 'c', 'c', 'd', 'e', 'a'];

$result = array_unique($arr);

print_r($result); // 输出:['a', 'b', 'c', 'd', 'e']
Copy after login

Method 2: Custom function

To keep the key name, we can write a custom function to deduplicate an array. This method uses an associative array and compares the hash of each value to determine if it is a duplicate.

Example:

function array_unique_preserve_keys($arr)
{
    $hash = [];
    $unique_arr = [];

    foreach ($arr as $key => $value)
    {
        $hash_value = md5($value);
        if (!isset($hash[$hash_value]))
        {
            $hash[$hash_value] = 1;
            $unique_arr[$key] = $value;
        }
    }

    return $unique_arr;
}

$arr = ['a', 'b', 'c', 'c', 'd', 'e', 'a'];

$result = array_unique_preserve_keys($arr);

print_r($result); // 输出:['a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd', 'e' => 'e']
Copy after login

Practical case:

Suppose we have an array from a form submission that contains duplicates Username and email address. By deduplicating it using the array_unique_preserve_keys() function, we can remove duplicate records while maintaining the user's username.

$form_data = [
    ['username' => 'john', 'email' => 'john@example.com'],
    ['username' => 'jane', 'email' => 'jane@example.com'],
    ['username' => 'john', 'email' => 'john@example.org'],
    ['username' => 'mark', 'email' => 'mark@example.net']
];

$unique_users = array_unique_preserve_keys($form_data);

print_r($unique_users); // 输出:['john' => ['username' => 'john', 'email' => 'john@example.com'], 'jane' => ['username' => 'jane', 'email' => 'jane@example.com'], 'mark' => ['username' => 'mark', 'email' => 'mark@example.net']]
Copy after login

The above is the detailed content of Best way to deduplicate arrays and keep key names 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