[精讚] [會員登入]
2451

[PHP] 移除陣列或字串中的重覆元素

移除陣列或字串中的重覆元素

分享此文連結 //n.sfs.tw/10901

分享連結 [PHP] 移除陣列或字串中的重覆元素@新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2019-10-23 16:25:49 最後編修
2017-03-14 22:48:48 By 張○○
 

自動目錄

字串 $str="1,2,1,3,4,2,2,5";  要移除重覆,只需要使用php內建的array_unique函數即可。

$arr= array_unique(explode(',',$str)); 

Array
(
    [0] => 1
    [1] => 2
    [3] => 3
    [4] => 4
    [7] => 5
)

如果要把他再組成字串,用implode就好了

$str=implode(',', $arr);

// 1,2,3,4,5

參考資料

[1] http://stackoverflow.com/questions/6438961/how-do-i-remove-duplicate-numbers


原文 2013-11-20 01:47:37

END

你可能感興趣的文章

[PHP+ci4] codeignitor4+ smarty5 這篇整合 php 的framework codeignitor4 + smarty5。

[CodeIgniter3] 解決無法上傳特定檔案(.sb2)的問題 上傳時出現The filetype you are attempting to upload is not allowed,要怎麼解決?

利用 php 木馬作為駭客攻擊的手法 利用 php 木馬作為駭客攻擊的手法

[PHP] 取得檔名和路徑:basename, dirname 由絕對路徑取的路徑及檔名的方法

[CodeIgniter 3] COOKIE的使用 PHP CodeIgniter 3 中COOKIE的使用超簡單

[Centos 6& 7] 安裝php-geoip php-geoip是PHP由domain、ip查詢城市國家資料非常強的函式

隨機好文

[Freebsd] 使用 ADSL 撥接上網 Freebsd上要使用 ADSL 撥接上網,該如何設定?

看懂DSUB DVI HDMI USB等各式影音接頭 看懂DSUB DVI HDMI等各式影音接頭

[Win7] 燒錄 iso 檔 在Windows7 中內建燒錄程式,可以直接把檔案拉到光碟機裡,再執行燒錄。

魔球中小女孩唱的歌 The show 魔球中小女孩唱的歌 The show

APACHE的記錄檔格式 LogFormat 語法 在APACHE中有定義一些記錄的語法模版 在 /etc/httpd/conf/httpd.conf 中: LogForm