php删除数组中重复值的代码
在$intTotal比较小的情况下,比如说1000以内,$intRand的取值基本不影响结果,两者执行的时间都差不多,测试$intTotal 大于10000时,$intRand取值100时,使用array_unique的效率要高于foreach循环判断,$intRand=10,两者执行时间一致.
PHP实例代码如下:
<?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>" ; //开源代码phpfensi测试数据 print_r( $arrT ); print_r( $arrRF ); print_r( $arrRS ); echo "</pre>" ; ?>因此,可以得出结论,当数组容量不大,大概在1000以内时,使用两者的执行效率差不多.
查看更多关于php删除数组中重复值的代码 - php数组的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did31229