In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "php how to achieve array de-duplication", in daily operation, I believe many people in php how to achieve array de-duplication problems have doubts, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "php how to achieve array de-duplication" doubts helpful! Next, please follow the small series to learn together!
Php array to achieve the method of duplication: 1, the use of array_unique method to duplicate the array elements;2, the use of array_flip method to duplicate.
Operating environment: Windows 7 system, PHP7.1 version, DELL G3 computer
Php array elements quickly de-duplication
1. Using array_unique method for deduplication
For de-duplication of array elements, we usually use the array_unique method, which can be used to de-duplicate elements in an array.
Output:
Array( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9)
After demultiplexing, the keys will not be in order, you can use array_values to reorder the keys.
2. Use array_unique method to remove duplication efficiency
unique count:99run time:653.39303016663msuse memory:5120kb
Using array_unique method to remove duplication, the running time takes about 650ms, and the memory consumption is about 5m.
3. Faster array de-duplication methods
Php has a key swap method array_flip, we can use this method to de-duplicate, because key swap, the original duplicate value will become the same key.
And then a key swap, the key and value for back to complete the de-duplication.
unique count:99run time:12.840032577515msuse memory:768kb
Using array_flip method to remove duplication, the running time takes about 18ms, and the memory consumption is about 2m.
Therefore, using array_flip method to reduce duplication than using array_unique method running time reduced by 98%, memory consumption reduced by 4/5;
At this point, the study of "how to achieve array de-duplication in php" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.