[精讚] [會員登入]
2482

[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] 隨機產生一個顏色字串 讓PHP 隨機產生一個顏色字串

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

[PHP] 字串編碼及解碼函式 為何要將字串編碼?理由很簡單,就是不要讓人家輕易的知道字串內容是什麼。例如點選分頁時,我們常會用這樣的連結: index

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

PHP 數字加解密函式 自寫的數字加解密,勉強用

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

隨機好文

[jQuery] 利用load()來達成ajax的寫法 jQuery中利用load()來達成ajax的寫法,也有人稱他是假的ajax,作法就是..

UTF-8 BOM (Byte Order Mark) 的問題 在 Michael Kaplan 那看到 Every character has a story #4: U+feff

SELinux 常用指令和檔案 在Redhat系列中,Centos5以後加入了selinux,他並沒有這麼可怕,不必每次看到Selinux ,就想把他

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

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