Remove duplicate values from PHP array _php tutorial

Source: Internet
Author: User
Remove duplicate values from an array, you can use the Foreach method, or you can use the Array_unique method, both of which use the following code.

 
  "; $intStart 1 = time (); $arrRS = Array_unique ($arrT); $intEnd 2 = time (); $intTime 2 = $intEnd 2-$intStart 1;echo" with Array_ Unique Function,spend Time: ($intTime 2) "; echo"
";p Rint_r ($arrT);p rint_r ($arrRF);p rint_r ($arrRS); echo"
";? >

In the case of small $inttotal, for example, within 1000, the value of $intRand basically does not affect the results, the two execution time is similar.

When the test $inttotal is greater than 10000, the efficiency of using array_unique is higher than that of the Foreach loop when the value of the $intRand is 100, $intRand = 10, and the execution time is consistent.

Therefore, it can be concluded that when the array size is not large, probably within 1000, the implementation efficiency is similar.

When the size of the array is large (what should be to what value, I have no detailed test, interested can determine this value), with the gradual increase of $intrand, array_unique performance better, I do not use $inttotal/$intRand this ratio, is because, The feeling is not proportional change, but the more basic will follow the ratio, array_unique performance better.

To sum up, in the filter array of duplicate values, it is recommended to use Array_unuique, when the array is not as efficient as the two, and array_unique use of course let your code suddenly reduced a few lines, the array capacity is too large, function performance better, why not?

http://www.bkjia.com/PHPjc/752465.html www.bkjia.com true http://www.bkjia.com/PHPjc/752465.html techarticle remove duplicate values from an array, you can use the Foreach method, or you can use the Array_unique method, both of which use the following code.? php$arrf = Array (); $arrS = Array (); $ ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.