search
HomeBackend DevelopmentPHP ProblemHow to quickly remove duplicate data in php

php method to remove duplicate data: 1. Use the "array_unique" method to deduplicate array elements, and use the "array_values" function to reorder the key values; 2. Use the "array_flip" method to deduplicate.

How to quickly remove duplicate data in php

Recommended: "PHP Video Tutorial"

php Quickly remove duplicates from array elements

1. Use the array_unique method to deduplicate

To deduplicate array elements, we generally use the array_unique method. This method can deduplicate the elements in the array.

<?php
$arr = array(1,1,2,3,3,3,4,4,5,6,6,7,8,8,9,9,9);
$arr = array_unique($arr);
$arr = array_values($arr);
print_r($arr);
?>

Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
)

After deduplication, the key values ​​will be out of order. You can use array_values ​​to reorder the key values.

2. Efficiency of using array_unique method to remove duplicates

<?php
$arr = array();
// 创建100000个随机元素的数组
for($i=0; $i<100000; $i++){
    $arr[] = mt_rand(1,99);
}
// 记录开始时间
$starttime = getMicrotime();
// 去重
$arr = array_unique($arr);
// 记录结束时间
$endtime = getMicrotime();
$arr = array_values($arr);
echo &#39;unique count:&#39;.count($arr).&#39;<br>&#39;;
echo &#39;run time:&#39;.(float)(($endtime-$starttime)*1000).&#39;ms<br>&#39;;
echo &#39;use memory:&#39;.getUseMemory();
/**
 * 获取使用内存
 * @return float
 */
function getUseMemory(){
    $use_memory = round(memory_get_usage(true)/1024,2).&#39;kb&#39;;
    return $use_memory;
}
/**
 * 获取microtime
 * @return float
 */
function getMicrotime(){
    list($usec, $sec) = explode(&#39; &#39;, microtime());
    return (float)$usec + (float)$sec;
}
?>
unique count:99
run time:653.39303016663ms
use memory:5120kb

Using array_unique method to remove duplicates, the running time takes about 650ms and the memory usage is about 5m

3. Faster array removal Duplication method

php has a key-value exchange method array_flip. We can use this method to remove duplicates. Because of key-value exchange, the original duplicate values ​​will become the same key.

Then perform a key-value exchange again, and exchange the keys and values ​​back to complete deduplication.

<?php
$arr = array();
// 创建100000个随机元素的数组
for($i=0; $i<100000; $i++){
    $arr[] = mt_rand(1,99);
}
// 记录开始时间
$starttime = getMicrotime();
// 使用键值互换去重
$arr = array_flip($arr);
$arr = array_flip($arr);
// 记录结束时间
$endtime = getMicrotime();
$arr = array_values($arr);
echo &#39;unique count:&#39;.count($arr).&#39;<br>&#39;;
echo &#39;run time:&#39;.(float)(($endtime-$starttime)*1000).&#39;ms<br>&#39;;
echo &#39;use memory:&#39;.getUseMemory();
/**
 * 获取使用内存
 * @return float
 */
function getUseMemory(){
    $use_memory = round(memory_get_usage(true)/1024,2).&#39;kb&#39;;
    return $use_memory;
}
/**
 * 获取microtime
 * @return float
 */
function getMicrotime(){
    list($usec, $sec) = explode(&#39; &#39;, microtime());
    return (float)$usec + (float)$sec;
}
?>
unique count:99
run time:12.840032577515ms
use memory:768kb

Using the array_flip method to remove duplicates takes about 18ms to run and takes up about 2m of memory.

Therefore, using the array_flip method to delete duplicates takes 98% less running time than using the array_unique method, and the memory usage is reduced by 4/ 5;

The above is the detailed content of How to quickly remove duplicate data in php. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.