Home>Article>Backend Development> How to quickly remove duplicate elements from php arrays

How to quickly remove duplicate elements from php arrays

墨辰丷
墨辰丷 Original
2018-05-22 16:55:33 1987browse

This article mainly introduces the method of quickly deduplicating PHP array elements, which has a good reference value. Let’s take a look at it with the editor

1. Use the array_unique method to deduplicate

To deduplicate array elements, we generally use the array_unique method, using This method can remove duplicate elements from the array.

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. Use array_unique method to remove duplicates for efficiency

'; 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; } ?>

unique count:99
run time:653.39303016663 ms
use memory:5120kb

Use thearray_uniquemethod to remove duplicates. The running time takes about650msand the memory usage is about5m

3. Faster array deduplication method

PHP has a key-value exchange method array_flip. We can use this method to deduplicate, because key-value exchange, The original duplicate value will become 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; } ?>

unique count:99
run time:12.840032577515ms
use memory:768kb

USEarray_flipMethod to remove duplicates, the running time takes about18ms, and the memory usage is about2m

, so using thearray_flipmethod to remove duplicates is better than usingarray_uniqueThe method running time is reduced by98%, and the memory usage is reduced by4/5;

Related recommendations:

How to use array_sum() to calculate the sum ofarray elementsvalue

php double quotes when accessingarray elementserror reporting How to handle

jquery operation objectArray elementMethod summary (with case)

The above is the detailed content of How to quickly remove duplicate elements from php arrays. 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