$arrF = array();
$arrS = array();
$intTotal = 1000;
$intRand = 10000;//数组元素值
//生成随机数组
for($i=0; $i < $intTotal; $i++){
$arrF[] = rand(1,$intRand);
$arrS[] = rand(1,$intRand);
}
$arrT = array_merge($arrF, $arrS);//把两个数组合并为一个数组
$arrRF = array();
$intStart = microtime_float();
//通过foreach循环去除重复数组
foreach($arrT as $v){
if(in_array($v, $arrRF)){
continue;
}else{
$arrRF[] = $v;
}
}
$intEnd = microtime_float();
$intTime = $intEnd-$intStart;//去重复所需时间
echo "通过foreach循环去除重复所需时间:$intTime
";
$intStart1 =microtime_float();
$arrRS = array_unique($arrT);
$intEnd2 = microtime_float();
$intTime2 = $intEnd2-$intStart1;
echo "通过array_unique函数去除数组重复执行时间:($intTime2)";
echo "
";
print_r($arrT);//原始数组
print_r($arrRF);//通过foreach去除重复后的数组
print_r($arrRS);//通过array_unique函数去除数组重复后的数组
echo "
";//取当前时间戳并精确到毫秒
function microtime_float(){
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
?>