PHP removes array duplicate values two method instances

Source: Internet
Author: User

PHP Two methods to remove duplicate values of the array, using the Foreach method and the Array_unique method respectively.

Remove duplicate values from an array, you can use the Foreach method, or you can use the Array_unique method.

<?PHP$arrF=Array();$arrS=Array();$intTotal= 100;$intRand= 10; for($i= 0;$i<$intTotal;$i++){$arrF[] =Rand(1,$intRand);$arrS[] =Rand(1,$intRand);}$arrT=Array_merge($arrF,$arrS);$arrRF=Array();$intStart= Time();foreach($arrT  as $v){if(In_array($v,$arrRF)){Continue;}Else{$arrRF[] =$v;}} Www.jbxue.com$intEnd= Time();$intTime=$intEnd-$intStart;Echo"With Continue,spend time:$intTime<br/> ";$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"<pre>";Print_r($arrT);Print_r($arrRF);Print_r($arrRS);Echo"</pre>";?>

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.

In summary, when filtering the 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 all of a sudden minus a few lines, the array capacity is too large, the performance of the function is better.

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.