[精讚] [會員登入]
2445

[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>7.3] switch中的 Did you mean to use "continue 2"? 除錯 這近更新 PHP到7.3版以後,出現這樣的錯:targeting switch is equivalent to "break". Did you mean to use "continue 2"?解決方法

[PHP] CodeIgniter 3+pure+smarty安裝及環境設置1/2 PHP framework CodeIgniter 3+ pure CSS +smarty Template Engine的整合

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

[PHP] 陣列排序 sort, ksort, asort, usort... 簡單的記錄PHP的陣列排序,因為常常會用到。

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

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

隨機好文

Smarty安裝 smarty 是著名的樣版引擎,非常的好用,用多了突然發現拿掉smarty反而不會寫php了,以下是安裝過程..

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

在Centos7 下安裝 Wildfly10 wildfly以前叫JBoss,2014.11.20改名叫Wildfly,起始版本是Wildfly8,現在已經出到Wil

讓radio box 或checkbox 好按 在行動裝置下,radio box及checkbox變得很難按得到

超扯童話血多-賣火柴小女孩 這個真的是太扯了,扯到一直笑,尤其是作者出來那段 因為他第一到第九會連播,笑滿累的。如果你看不懂的話表示你的大腦還滿正經