新聞中心
php如何刪除兩個數(shù)組中相同的元素
php如何去除數(shù)組中相同的元素
公司主營業(yè)務(wù):成都網(wǎng)站制作、網(wǎng)站設(shè)計、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出東山免費(fèi)做網(wǎng)站回饋大家。
如何去除數(shù)組中相同的元素?在php手冊中有這樣寫
array_unique(PHP
4
=
4.0.1)
array_unique
--
移除數(shù)組中重復(fù)的值
說明
array
array_unique
(
array
array)
array_unique()
接受
array
作為輸入并返回沒有重復(fù)值的新數(shù)組。
注意鍵名保留不變。array_unique()
先將值作為字符串排序,然后對每個值只保留第一個遇到的鍵名,接著忽略所有后面的鍵名。這并不意味著在未排序的
array
中同一個值的第一個出現(xiàn)的鍵名會被保留。
注:
當(dāng)且僅當(dāng)
(string)
$elem1
===
(string)
$elem2
時兩個單元被認(rèn)為相同。就是說,當(dāng)字符串的表達(dá)一樣時。
第一個單元將被保留。
例子
1.
array_unique()
例子
?php
$input
=
array
("a"
=
"green",
"red",
"b"
=
"green",
"blue",
"red");
$result
=
array_unique
($input);
print_r($result);
?
php數(shù)組怎么去除重復(fù)的數(shù)組值
array_unique() 函數(shù)定義和用法
移除數(shù)組中的重復(fù)的值,并返回結(jié)果數(shù)組。
當(dāng)幾個數(shù)組元素的值相等時,只保留第一個元素,其他的元素被刪除。
返回的數(shù)組中鍵名不變。
例子
?php
$a=array("a"="Cat","b"="Dog","c"="Cat");
print_r(array_unique($a));
?
輸出:Array ( [a] = Cat [b] = Dog )
如何正確實現(xiàn)PHP刪除數(shù)組重復(fù)元素
array_unique
(PHP 4 = 4.0.1, PHP 5, PHP 7)
array_unique — 移除數(shù)組中重復(fù)的值
說明
array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
array_unique() 接受 array 作為輸入并返回沒有重復(fù)值的新數(shù)組。
注意鍵名保留不變。array_unique() 先將值作為字符串排序,然后對每個值只保留第一個遇到的鍵名,接著忽略所有后面的鍵名。這并不意味著在未排序的 array 中同一個值的第一個出現(xiàn)的鍵名會被保留。
Note: 當(dāng)且僅當(dāng) (string) $elem1 === (string) $elem2 時兩個單元被認(rèn)為相同。就是說,當(dāng)字符串的表達(dá)一樣時。 第一個單元將被保留。
參數(shù)
array
輸入的數(shù)組。
sort_flags
The optional second parameter sort_flags may be used to modify the sorting behavior using these values:
Sorting type flags:
SORT_REGULAR - compare items normally (don't change types)
SORT_NUMERIC - compare items numerically
SORT_STRING - compare items as strings
SORT_LOCALE_STRING - compare items as strings, based on the current locale.
返回值
Returns the filtered array.
更新日志
版本
說明
5.2.10 Changed the default value of sort_flags back to SORT_STRING.
5.2.9 Added the optional sort_flags defaulting to SORT_REGULAR. Prior to 5.2.9, this function used to sort the array with SORT_STRING internally.
范例
Example #1 array_unique() 例子
?php
$input = array("a" = "green", "red", "b" = "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?
以上例程會輸出:
Array
(
[a] = green
[0] = red
[1] = blue
)
Example #2 array_unique() 和類型
?php
$input = array(4, "4", "3", 4, 3, "3");
$result = array_unique($input);
var_dump($result);
?
以上例程會輸出:
array(2) {
[0] = int(4)
[2] = string(1) "3"
}
參見
array_count_values() - 統(tǒng)計數(shù)組中所有的值出現(xiàn)的次數(shù)
注釋
Note: Note that array_unique() is not intended to work on multi dimensional arrays.
php數(shù)組去除重復(fù)的值
?php
$array?=?array(
array(
'id'?=?19,
'title'?=?'總統(tǒng)套房'
),
array(
'id'?=?20,
'title'?=?'豪華套房'
),
array(
'id'?=?21,
'title'?=?'豪華套房'
),
array(
'id'?=?22,
'title'?=?'總統(tǒng)套房'
),
);
foreach?($array?as?$key?=?$value)?{
foreach?($value?as?$k?=?$v)?{
if?($k?==?'title')?{
$new_arr[]?=?$v;
}
}
}
$arr?=?array_unique($new_arr);
echo?implode(',',?$arr);
?
望采納 Thx
php 去掉完全相同的重復(fù)數(shù)組
一、這個沒有被合并,只是取的后面這個鍵名的值,
二、$input=array("11"="aaaa","22"="bbbb","33"="cccc","11"="aaada","44"="cccc1","55"="cccc");
$result
=
array_unique
($input);
print_r($result);
輸出的結(jié)果:Array
(
[11]
=
aaada
[22]
=
bbbb
[33]
=
cccc
[44]
=
cccc1
)
鍵名33
和
55
的值完全一樣的時候,后者會被干掉
如果你要的是鍵名和值完全一致的時候才刪除一個的話,似乎不能,因為鍵名是不允許重復(fù)的
聽你的情況似乎數(shù)據(jù)量很大,建議你使用
array_flip()函數(shù)
【php中,刪除數(shù)組中重復(fù)元素有一個可用的函數(shù),那就是array_unique(),
但是它并不是一個最高效的方法,使用array_flip()函數(shù)將比array_uniqure()在速度上高出五倍左右。】
例子:$input=array("11"="aaaa","22"="bbbb","33"="cccc","11"="aaada","44"="cccc1","55"="cccc");
$arr1
=
array_flip(array_flip($input));
print_r($arr1);
輸出的結(jié)果:Array
(
[11]
=
aaada
[22]
=
bbbb
[55]
=
cccc
[44]
=
cccc1
)
標(biāo)題名稱:php去除數(shù)組重復(fù)數(shù)據(jù),php字符串去重
新聞來源:http://fisionsoft.com.cn/article/dsihhie.html