Home Backend Development PHP Problem How to delete duplicate records in an array in php and keep the latest data

How to delete duplicate records in an array in php and keep the latest data

Apr 23, 2023 pm 04:43 PM

In our daily development, array deduplication is an operation that is often used. Of course, it is not very difficult to remove duplicates from an ordinary array. We can use foreach to traverse the array and then add the non-duplicate parts to a new array. However, when the same elements appear in the array, we usually need to keep the latest one and delete other duplicate elements. So, how to achieve this?

This article will introduce a method to use PHP language to delete duplicate elements of an array and retain the latest records. We will implement it in the following three steps:

1. Use the array_unique function to remove duplicates

array_unique() function can deduplicate the array, and it will automatically identify duplicate elements in the array and delete. For example:

$arr = array(1, 2, 3, 3, 4, 4, 5);
print_r(array_unique($arr)); // 输出 [1, 2, 3, 4, 5]
Copy after login

However, after using the array_unique function, the elements in the array are not kept latest according to our needs, so what should we do?

2. Use the array_reverse function to reverse the array

If we can sort the array in reverse order, we can retain the latest elements after deduplication. We can use the array_reverse function to sort an array in reverse order. For example:

$arr = array(1, 2, 3, 3, 4, 4, 5);
$arr = array_reverse($arr);
print_r($arr); // 输出 [5, 4, 4, 3, 3, 2, 1]
Copy after login

3. Use foreach loop and unset function to delete duplicate elements

After the above two steps, we have sorted the array in reverse order and can retain the latest elements. Next, you can use the foreach loop and the unset function to remove duplicate elements. The specific operations are as follows:

$arr = array(1, 2, 3, 3, 4, 4, 5);
$arr = array_reverse($arr);

$unique = array();
foreach ($arr as $key=>$value) {
    if (!in_array($value, $unique)) {
        $unique[] = $value;
    } else {
        // 删除重复元素
        unset($arr[$key]);
    }
}

// 最后再将数组逆序回来
$arr = array_reverse($arr);
print_r($arr); // 输出 [1, 2, 3, 4, 5]
Copy after login

Through the above operations, we have successfully implemented the function of deleting duplicate elements of the array and retaining the latest records. Although this method involves some operations of sorting the array in reverse order and deleting elements, the whole process is not complicated and is suitable for various PHP development scenarios.

The above is the detailed content of How to delete duplicate records in an array in php and keep the latest data. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the best practices for deduplication of PHP arrays What are the best practices for deduplication of PHP arrays Mar 03, 2025 pm 04:41 PM

What are the best practices for deduplication of PHP arrays

Can PHP array deduplication take advantage of key name uniqueness? Can PHP array deduplication take advantage of key name uniqueness? Mar 03, 2025 pm 04:51 PM

Can PHP array deduplication take advantage of key name uniqueness?

What Are the Latest PHP Coding Standards and Best Practices? What Are the Latest PHP Coding Standards and Best Practices? Mar 10, 2025 pm 06:16 PM

What Are the Latest PHP Coding Standards and Best Practices?

Does PHP array deduplication need to be considered for performance losses? Does PHP array deduplication need to be considered for performance losses? Mar 03, 2025 pm 04:47 PM

Does PHP array deduplication need to be considered for performance losses?

How Do I Work with PHP Extensions and PECL? How Do I Work with PHP Extensions and PECL? Mar 10, 2025 pm 06:12 PM

How Do I Work with PHP Extensions and PECL?

How to Implement message queues (RabbitMQ, Redis) in PHP? How to Implement message queues (RabbitMQ, Redis) in PHP? Mar 10, 2025 pm 06:15 PM

How to Implement message queues (RabbitMQ, Redis) in PHP?

What are the optimization techniques for deduplication of PHP arrays What are the optimization techniques for deduplication of PHP arrays Mar 03, 2025 pm 04:50 PM

What are the optimization techniques for deduplication of PHP arrays

How to Use Reflection to Analyze and Manipulate PHP Code? How to Use Reflection to Analyze and Manipulate PHP Code? Mar 10, 2025 pm 06:12 PM

How to Use Reflection to Analyze and Manipulate PHP Code?

See all articles