如何从PHP中删除数组中的重复值
如何从PHP中删除数组中的重复值?
21个解决方案
204 votes
使用array_unique()。
例:
$array = array(1, 2, 2, 3);
$array = array_unique($array); // Array is now (1, 2, 3)
Jeremy Ruten answered 2019-05-30T16:42:42Z
21 votes
//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 )
chim answered 2019-05-30T16:42:59Z
15 votes
使用array_unique
array_unique:用于唯一数组array_values:用于重建索引
nimey sara thomas answered 2019-05-30T16:43:32Z
5 votes
有时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
结果
array ( 0 => '', 1 => 'A1', 2 => 'A2', ) /* $unique */
array ( 0 => '', 1 => '', 2 => '', 3 => 'A1', ) /* $duplicated */
AgelessEssence answered 2019-05-30T16:44:04Z
4 votes
唯一对我有用的是:
$array = array_unique($array, SORT_REGULAR);
niravpatel9898 answered 2019-05-30T16:44:29Z
4 votes
$result = array();
foreach ($array as $key => $value){
if(!in_array($value, $result))
$result[$key]=$value;
}
Amr Berag answered 2019-05-30T16:44:48Z
2 votes
explode(",", implode(",", array_unique(explode(",", $YOUR_ARRAY))));
这将处理关键关联并序列化生成的新数组的键:-)
Deb answered 2019-05-30T16:45:22Z
2 votes
我们可以创建这种类型的数组来使用这个最后的值将更新为列或键值,我们将从数组中获取唯一值...
$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);
harsh kumar answered 2019-05-30T16:45:49Z
1 votes
这是一个很好的方法。 可能要确保其输出再次返回数组。 现在,您只显示最后一个唯一值。
试试这个:
$arrDuplicate = array ("","",1,3,"",5);
foreach (array_unique($arrDuplicate) as $v){
if($v != "") { $arrRemoved[] = $v; }
}
print_r ($arrRemoved);
Dries B answered 2019-05-30T16:46:16Z
1 votes
我发现,根据你的阵列的大小
$array = array_values( array_flip( array_flip( $array ) ) );
可以比array_unique更快。
Bollis answered 2019-05-30T16:46:49Z
1 votes
if (@!in_array($classified->category,$arr)){
$arr[] = $classified->category;
?>
第一次检查数组中的值并找到相同的值忽略它
Alpesh Navadiya answered 2019-05-30T16:47:16Z
1 votes
从PHP中的关联数组中删除重复值。
$arrDup = Array ('0' => 'aaa-aaa' , 'SKU' => 'aaa-aaa' , '1' => '12/1/1' , 'date' => '12/1/1' , '2' => '1.15' , 'cost' => '1.15' );
foreach($arrDup as $k => $v){
if(!( isset ($hold[$v])))
$hold[$v]=1;
else
unset($arrDup[$k]);
}
数组([0] => aaa-aaa [1] => 12/1/1 [2] => 1.15)
Shivivanand answered 2019-05-30T16:47:48Z
0 votes
$arrDuplicate = array ("","",1,3,"",5);
foreach(array_unique($arrDuplicate) as $v){
if($v != "" ){$arrRemoved = $v; }}
print_r($arrRemoved);
user1045247 answered 2019-05-30T16:48:06Z
0 votes
function arrayUnique($myArray)
{
$newArray = Array();
if (is_array($myArray))
{
foreach($myArray as $key=>$val)
{
if (is_array($val))
{
$val2 = arrayUnique($val);
}
else
{
$val2 = $val;
$newArray=array_unique($myArray);
$newArray=deleteEmpty($newArray);
break;
}
if (!empty($val2))
{
$newArray[$key] = $val2;
}
}
}
return ($newArray);
}
function deleteEmpty($myArray)
{
$retArray= Array();
foreach($myArray as $key=>$val)
{
if (($key<>"") && ($val<>""))
{
$retArray[$key] &#61; $val;
}
}
return $retArray;
}
Vineesh Kalarickal answered 2019-05-30T16:48:25Z
0 votes
试试这个简短的&#xff06;amp; 甜蜜的代码 -
$array &#61; array (1,4,2,1,7,4,9,7,5,9);
$unique &#61; array();
foreach($array as $v){
isset($k[$v]) || ($k[$v]&#61;1) && $unique[] &#61; $v;
}
var_dump($unique);
输出 -
array(6) {
[0]&#61;>
int(1)
[1]&#61;>
int(4)
[2]&#61;>
int(2)
[3]&#61;>
int(7)
[4]&#61;>
int(9)
[5]&#61;>
int(5)
}
Rohit Suthar answered 2019-05-30T16:48:53Z
0 votes
$arr1 &#61; [1,1,2,3,4,5,6,3,1,3,5,3,20];
print_r(arr_unique($arr1));
function arr_unique($arr) {
sort($arr);
$curr &#61; $arr[0];
$uni_arr[] &#61; $arr[0];
for($i&#61;0; $i
if($curr !&#61; $arr[$i]) {
$uni_arr[] &#61; $arr[$i];
$curr &#61; $arr[$i];
}
}
return $uni_arr;
}
sumityadavbadli answered 2019-05-30T16:49:11Z
0 votes
有多种方法可以做到这些&#xff0c;如下所示
//first method
$filter &#61; array_map("unserialize", array_unique(array_map("serialize", $arr)));
//second method
$array &#61; array_unique($arr, SORT_REGULAR);
Shahrukh Anwar answered 2019-05-30T16:49:36Z
0 votes
如果您关注性能并且具有简单的数组&#xff0c;请使用&#xff1a;
array_keys(array_flip($array));
它比array_unique快很多倍。
jabko87 answered 2019-05-30T16:50:09Z
0 votes
在这里&#xff0c;我创建了第二个空数组&#xff0c;并使用for循环与第一个具有重复的数组。 它将运行与第一个数组的计数一样多的时间。 然后与具有第一个数组的数组的位置进行比较&#xff0c;并通过使用in_array匹配它已经或不具有此项目。如果没有&#xff0c;那么它将使用array_push将该项添加到第二个数组。
$a &#61; array(1,2,3,1,3,4,5);
$count &#61; count($a);
$b &#61; [];
for($i&#61;0; $i
if(!in_array($a[$i], $b)){
array_push($b, $a[$i]);
}
}
print_r ($b);
Aladin Banwal answered 2019-05-30T16:50:36Z
0 votes
它可以通过函数完成我做了三个函数重复返回数组中重复的值。
第二个函数single只返回那些单个均值不在数组中重复的值&#xff0c;第三个和完整函数返回所有值但如果任何值重复则不重复它将其转换为单个;
function duplicate($arr) {
$duplicate;
$count &#61; array_count_values($arr);
foreach($arr as $key &#61;> $value) {
if ($count[$value] > 1) {
$duplicate[$value] &#61; $value;
}
}
return $duplicate;
}
function single($arr) {
$single;
$count &#61; array_count_values($arr);
foreach($arr as $key &#61;> $value) {
if ($count[$value] &#61;&#61; 1) {
$single[$value] &#61; $value;
}
}
return $single;
}
function full($arr, $arry) {
$full &#61; $arr &#43; $arry;
sort($full);
return $full;
}
Mirza Obaid answered 2019-05-30T16:51:09Z
-1 votes
我没有使用任何功能就这样做了。
$arr &#61; array("1", "2", "3", "4", "5", "4", "2", "1");
$len &#61; count($arr);
for ($i &#61; 0; $i <$len; $i&#43;&#43;) {
$temp &#61; $arr[$i];
$j &#61; $i;
for ($k &#61; 0; $k <$len; $k&#43;&#43;) {
if ($k !&#61; $j) {
if ($temp &#61;&#61; $arr[$k]) {
echo $temp."
";
$arr[$k]&#61;" ";
}
}
}
}
for ($i &#61; 0; $i <$len; $i&#43;&#43;) {
echo $arr[$i] . "
";
}
Ashishdmc4 answered 2019-05-30T16:51:34Z