[精讚] [會員登入]
2695

[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 移除陣列中的元素 要移除陣列中的其中一項元素

[PHP] UTF8中取出字串中特定的字數 要將字串,例如資料庫取出的TEXT,取出特定的字數

[PHP] 位元運算 (Bitwise operation)及和PERL比較 PHP 的位元運算,及和PERL比較整理

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

設定Google analytics API #2 -- PHP的程式安裝和撰寫 讓你的網站能夠存取你的Google analytics上面的資料

隨機好文

PHP for sphinx 函式庫安裝 PECL/sphinx PHP>= 5.2.2 已經能原生支援 sphinx,可是預設的沒有裝,我們得自己裝才能用

世紀帝國征服者新版本--被遺忘的帝國 世紀帝國征服者新版本--被遺忘的帝國 世紀二代的征服者是精典遊戲中的精典,aofe更好玩...

[Wildfly10] 發佈war檔 deploy war file onto wildfly10

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

[Centos6] 安裝wordpress Centos 6下安裝wordpress,非常簡單