Home > Backend Development > PHP Tutorial > Comparison of two methods of removing duplicate values ​​from arrays in PHP_PHP Tutorial

Comparison of two methods of removing duplicate values ​​from arrays in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:24:41
Original
704 people have browsed it

To remove duplicate values ​​from an array, you can use the foreach method or the array_unique method. The following code uses both methods.

<&#63;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;
	}
}
$intEnd = time();
$intTime = $intEnd-$intStart;
echo "With Continue,Spend time:$intTime<br/>";
$intStart1 = time();
$arrRS = array_unique($arrT);
$intEnd2 = time();
$intTime2 = $intEnd2-$intStart1;
echo "With array_unique function,Spend time:($intTime2)";
echo "<pre class="brush:php;toolbar:false">";
print_r($arrT);
print_r($arrRF);
print_r($arrRS);
echo "
"; ?>
Copy after login

When $intTotal is relatively small, for example, within 1000, the value of $intRand basically does not affect the result, and the execution time of both is similar.

When testing $intTotal is greater than 10000 and $intRand is 100, the efficiency of using array_unique is higher than that of foreach loop judgment. $intRand=10, the execution time of the two is consistent.

Therefore, it can be concluded that when the array capacity is not large, probably within 1000, the execution efficiency of using the two is similar.

When the array capacity is relatively large (I have not tested the specific value, you can determine this value if you are interested), as $intRand gradually increases, array_unique performs better, I do not use $ The reason for the ratio of intTotal/$intRand is that it does not feel proportional to the change, but it basically follows that the larger the ratio, the better the performance of array_unique.

To sum up, when filtering duplicate values ​​in an array, it is recommended to use array_unuique. When the array is small, the two are equally efficient. Using array_unique will of course reduce your code by several lines. When the array capacity is too large, , the function performs better, why not use it?

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825381.htmlTechArticleTo remove duplicate values ​​in an array, you can use the foreach method or the array_unique method. There are two types of code below. All methods have been used. php$arrF = array();$arrS = array();$i...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template