[精讚] [會員登入]
2415

[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] 取得檔名和路徑:basename, dirname 由絕對路徑取的路徑及檔名的方法

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

[phpmyadmin] 缺少 mcrypt 外掛,請檢查 PHP 設定 缺少 mcrypt 外掛,請檢查 PHP 設定,安裝php-mcrypt。

[PHP] 檢查IP是否在某個網段內 mtachcidr 要檢查IP是否在某個網段內,要寫幾行?10行?5行? 不用,只要2行。以下是我寫的 code /** * matchCI

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

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

隨機好文

[jQuery] select 元件的取值及給值 html中的元件select,在jquery中要如何使用?

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

維修冰箱 維修冰箱

[NetBeans] 使用git複製別人的專案 NetBeans使用git複製別人的專案及版本控制

[AS3] 變數型態 基本類型宣告 as3 有下列幾種基本類型:string, int, number, object, boolean, n