這篇文章主要介紹“php如何實(shí)現(xiàn)數(shù)組去重”,在日常操作中,相信很多人在php如何實(shí)現(xiàn)數(shù)組去重問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”php如何實(shí)現(xiàn)數(shù)組去重”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
成都創(chuàng)新互聯(lián)成立于2013年,我們提供高端網(wǎng)站建設(shè)公司、成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)站定制、全網(wǎng)營(yíng)銷推廣、成都小程序開發(fā)、微信公眾號(hào)開發(fā)、seo優(yōu)化排名服務(wù),提供專業(yè)營(yíng)銷思路、內(nèi)容策劃、視覺設(shè)計(jì)、程序開發(fā)來完成項(xiàng)目落地,為紙箱企業(yè)提供源源不斷的流量和訂單咨詢。
php實(shí)現(xiàn)數(shù)組去重的方法:1、使用array_unique方法對(duì)數(shù)組元素進(jìn)行去重;2、使用array_flip方法進(jìn)行去重。
本文操作環(huán)境:windows7系統(tǒng)、PHP7.1版、DELL G3電腦
php 數(shù)組元素快速去重
1.使用array_unique方法進(jìn)行去重
對(duì)數(shù)組元素進(jìn)行去重,我們一般會(huì)使用array_unique方法,使用這個(gè)方法可以把數(shù)組中的元素去重。
<?php $arr = array(1,1,2,3,3,3,4,4,5,6,6,7,8,8,9,9,9); $arr = array_unique($arr); $arr = array_values($arr); print_r($arr); ?>
輸出:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 )
去重后,鍵值會(huì)不按順序,可以使用array_values把鍵值重新排序。
2.使用array_unique方法去重效率
<?php $arr = array(); // 創(chuàng)建100000個(gè)隨機(jī)元素的數(shù)組 for($i=0; $i<100000; $i++){ $arr[] = mt_rand(1,99); } // 記錄開始時(shí)間 $starttime = getMicrotime(); // 去重 $arr = array_unique($arr); // 記錄結(jié)束時(shí)間 $endtime = getMicrotime(); $arr = array_values($arr); echo 'unique count:'.count($arr).'<br>'; echo 'run time:'.(float)(($endtime-$starttime)*1000).'ms<br>'; echo 'use memory:'.getUseMemory(); /** * 獲取使用內(nèi)存 * @return float */ function getUseMemory(){ $use_memory = round(memory_get_usage(true)/1024,2).'kb'; return $use_memory; } /** * 獲取microtime * @return float */ function getMicrotime(){ list($usec, $sec) = explode(' ', microtime()); return (float)$usec + (float)$sec; } ?>
unique count:99 run time:653.39303016663ms use memory:5120kb
使用array_unique方法去重,運(yùn)行時(shí)間需要約650ms,內(nèi)存占用約5m
3.更快的數(shù)組去重方法
php有一個(gè)鍵值互換的方法array_flip,我們可以使用這個(gè)方法去重,因?yàn)殒I值互換,原來重復(fù)的值會(huì)變?yōu)橄嗤逆I。
然后再進(jìn)行一次鍵值互換,把鍵和值換回來則可以完成去重。
<?php $arr = array(); // 創(chuàng)建100000個(gè)隨機(jī)元素的數(shù)組 for($i=0; $i<100000; $i++){ $arr[] = mt_rand(1,99); } // 記錄開始時(shí)間 $starttime = getMicrotime(); // 使用鍵值互換去重 $arr = array_flip($arr); $arr = array_flip($arr); // 記錄結(jié)束時(shí)間 $endtime = getMicrotime(); $arr = array_values($arr); echo 'unique count:'.count($arr).'<br>'; echo 'run time:'.(float)(($endtime-$starttime)*1000).'ms<br>'; echo 'use memory:'.getUseMemory(); /** * 獲取使用內(nèi)存 * @return float */ function getUseMemory(){ $use_memory = round(memory_get_usage(true)/1024,2).'kb'; return $use_memory; } /** * 獲取microtime * @return float */ function getMicrotime(){ list($usec, $sec) = explode(' ', microtime()); return (float)$usec + (float)$sec; } ?>
unique count:99 run time:12.840032577515ms use memory:768kb
使用array_flip方法去重,運(yùn)行時(shí)間需要約18ms,內(nèi)存占用約2m
因此使用array_flip方法去重比使用array_unique方法運(yùn)行時(shí)間減少98%,內(nèi)存占用減少4/5;
到此,關(guān)于“php如何實(shí)現(xiàn)數(shù)組去重”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
網(wǎng)頁(yè)名稱:php如何實(shí)現(xiàn)數(shù)組去重
網(wǎng)站URL:http://www.ekvhdxd.cn/article4/jcgdoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站排名、網(wǎng)站營(yíng)銷、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)