How to implement array deduplication in php

藏色散人
Release: 2023-03-14 19:44:02
Original
11262 people have browsed it

php method to implement array deduplication: 1. Use the array_unique method to deduplicate array elements; 2. Use the array_flip method to deduplicate.

How to implement array deduplication in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

php array element quick deduplication

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.

Copy after login

Output:

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

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

'; echo 'run time:'.(float)(($endtime-$starttime)*1000).'ms
'; echo 'use memory:'.getUseMemory(); /** * 获取使用内存 * @return float */ function getUseMemory(){ $use_memory = round(memory_get_usage(true)/1024,2).'kb'; return $use_memory; } /** * 获取microtime * @return float */ function getMicrotime(){ list($usec, $sec) = explode(' ', microtime()); return (float)$usec + (float)$sec; } ?>
Copy after login
unique count:99 run time:653.39303016663ms use memory:5120kb
Copy after login

Using array_unique method to remove duplicates takes about 650ms to run and takes up about 5m of memory

3. Faster array deduplication method

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

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

'; echo 'run time:'.(float)(($endtime-$starttime)*1000).'ms
'; echo 'use memory:'.getUseMemory(); /** * 获取使用内存 * @return float */ function getUseMemory(){ $use_memory = round(memory_get_usage(true)/1024,2).'kb'; return $use_memory; } /** * 获取microtime * @return float */ function getMicrotime(){ list($usec, $sec) = explode(' ', microtime()); return (float)$usec + (float)$sec; } ?>
Copy after login
unique count:99 run time:12.840032577515ms use memory:768kb
Copy after login

Using the array_flip method to deduplicate, the running time takes about 18ms, and the memory usage is about 2m

Therefore, using the array_flip method to deduplicate the running time is 98% shorter than using the array_unique method, and the memory usage is reduced by 4/ 5;

Recommended learning: "PHP Video Tutorial"

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

Related labels:
php
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!