Php array element de-duplication, php array element
1. remove duplicates using the array_unique Method
To deduplicate array elements, we usually use the array_unique method. This method can be used to 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 are not sorted in order. You can use array_values to re-sort the key values.
2. De-duplication efficiency using the array_unique Method
<? Php $ arr = array (); // create an array of 100000 random elements for ($ I = 0; $ I <100000; $ I ++) {$ arr [] = mt_rand ();} // record start time $ starttime = getMicrotime (); // deduplication $ arr = array_unique ($ arr ); // record end time $ endtime = getMicrotime (); $ arr = array_values ($ arr); echo 'unique count :'. count ($ arr ). '<br>'; echo 'run time :'. (float) ($ endtime-$ starttime) * 1000 ). 'Ms <br> '; echo 'use memory :'. getUseMemory ();/*** get memory usage * @ return float */function getUseM Emory () {$ use_memory = round (memory_get_usage (true)/1024,2 ). 'kb'; return $ use_memory;}/*** get 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
The array_unique method is used to remove duplicates. The running time is about 650 ms, and the memory usage is about 5 m.
3. Faster array deduplication Method
PHP has a key-value interchange method, array_flip. We can use this method to remove duplicates. Because of key-value SWAps, the original duplicate values will become the same key.
Then perform another key-value swap. You can replace the key and value to complete deduplication.
<? Php $ arr = array (); // create an array of 100000 random elements for ($ I = 0; $ I <100000; $ I ++) {$ arr [] = mt_rand ();} // record start time $ starttime = getMicrotime (); // use key-value swaps to remove duplicates $ arr = array_flip ($ arr); // record end time $ endtime = getMicrotime (); $ arr = array_values ($ arr); echo 'unique count :'. count ($ arr ). '<br>'; echo 'run time :'. (float) ($ endtime-$ starttime) * 1000 ). 'Ms <br> '; echo 'use memory :'. getUseMemory ();/*** get memory usage * @ ret Urn float */function getUseMemory () {$ use_memory = round (memory_get_usage (true)/1024,2 ). 'kb'; return $ use_memory;}/*** get microtime * @ return float */function getMicrotime () {list ($ usec, $ sec) = explode ('', microtime (); return (float) $ usec + (float) $ sec;}?>
Unique count: 99
Run time: 12.840032577515 ms
Use memory: 768kb
The array_flip method is used to remove duplicates. The running time is about 18 ms, and the memory usage is about 2 m.
Therefore, the array_flip method is less than the array_unique method, and the memory usage is reduced by 98%;
The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!