php 如何从PHP中的数组中删除重复值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/307650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove duplicate values from an array in PHP
提问by Ian
How can I remove duplicate values from an array in PHP?
如何从 PHP 中的数组中删除重复值?
回答by Jeremy Ruten
Use array_unique().
Example:
例子:
$array = array(1, 2, 2, 3);
$array = array_unique($array); // Array is now (1, 2, 3)
回答by nimey sara thomas
Use array_values(array_unique($array));
用 array_values(array_unique($array));
array_unique: for unique array
array_values: for reindexing
array_unique:用于唯一数组
array_values:用于重新索引
回答by chim
//Find duplicates
$arr = array(
'unique',
'duplicate',
'distinct',
'justone',
'three3',
'duplicate',
'three3',
'three3',
'onlyone'
);
$unique = array_unique($arr);
$dupes = array_diff_key( $arr, $unique );
// array( 5=>'duplicate', 6=>'three3' 7=>'three3' )
// count duplicates
array_count_values($dupes); // array( 'duplicate'=>1, 'three3'=>2 )
回答by iniravpatel
The only thing which worked for me is:
唯一对我有用的是:
$array = array_unique($array, SORT_REGULAR);
Edit : SORT_REGULARkeeps the same order of the original array.
编辑:SORT_REGULAR保持原始数组的相同顺序。
回答by Amr Berag
$result = array();
foreach ($array as $key => $value){
if(!in_array($value, $result))
$result[$key]=$value;
}
回答by AgelessEssence
sometimes array_unique()is not the way,
if you want get unique AND duplicated items...
有时array_unique()不是这样,如果你想获得独特和重复的项目......
$unique=array("","A1","","A2","","A1","");
$duplicated=array();
foreach($unique as $k=>$v) {
if( ($kt=array_search($v,$unique))!==false and $k!=$kt )
{ unset($unique[$kt]); $duplicated[]=$v; }
}
sort($unique); // optional
sort($duplicated); // optional
results on
结果在
array ( 0 => '', 1 => 'A1', 2 => 'A2', ) /* $unique */
array ( 0 => '', 1 => '', 2 => '', 3 => 'A1', ) /* $duplicated */
回答by harsh kumar
We can create such type of array to use this last value will be updated into column or key value and we will get unique value from the array...
我们可以创建这种类型的数组来使用最后一个值将被更新为列或键值,我们将从数组中获得唯一值...
$array = array (1,3,4,2,1,7,4,9,7,5,9);
$data=array();
foreach($array as $value ){
$data[$value]= $value;
}
array_keys($data);
OR
array_values($data);
回答by Deb
explode(",", implode(",", array_unique(explode(",", $YOUR_ARRAY))));
explode(",", implode(",", array_unique(explode(",", $YOUR_ARRAY))));
This will take care of key associations and serialize the keys for the resulting new array :-)
这将处理键关联并序列化生成的新数组的键:-)
回答by Bollis
Depending on the size of your array, I have found
根据您的阵列的大小,我发现
$array = array_values( array_flip( array_flip( $array ) ) );
can be faster than array_unique.
可以比 array_unique 快。
回答by pawan kumar
$a = array(1, 2, 3, 4);
$b = array(1, 6, 5, 2, 9);
$c = array_merge($a, $b);
$unique = array_keys(array_flip($c));
print_r($unique);

