php过滤数组中重复的值_php去除数组重复值的两种方法及效率比较

$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);

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值