如何删除数组中的重复项?

时间:2023-02-14 04:18:33

How can I delete duplicates in array?

如何删除数组中的重复项?

For example if I had the following array:

例如,如果我有以下数组:

$array = array('1','1','2','3');

I want it to become

我希望它成为

$array = array('2','3');

so I want it to delete the whole value if two of it are found

所以如果找到其中两个,我希望它删除整个值

5 个解决方案

#1


4  

Depending on PHP version, this should work in all versions of PHP >= 4.0.6 as it doesn't require anonymous functions that require PHP >= 5.3:

根据PHP版本,这应该适用于所有版本的PHP> = 4.0.6,因为它不需要需要PHP> = 5.3的匿名函数:

function moreThanOne($val) {
    return $val < 2;
}


$a1 = array('1','1','2','3');
print_r(array_keys(array_filter(array_count_values($a1), 'moreThanOne')));

DEMO (Change the PHP version in the drop-down to select the version of PHP you are using)

DEMO(更改下拉列表中的PHP版本以选择您正在使用的PHP版本)

This works because:

这是因为:

  1. array_count_values will go through the array and create an index for each value and increment it each time it encounters it again.
  2. array_count_values将遍历数组并为每个值创建一个索引,并在每次遇到它时递增它。

  3. array_filter will take the created array and pass it through the moreThanOne function defined earlier, if it returns false, the key/value pair will be removed.
  4. array_filter将获取创建的数组并将其传递给之前定义的moreThanOne函数,如果返回false,则将删除键/值对。

  5. array_keys will discard the value portion of the array creating an array with the values being the keys that were defined. This final step gives you a result that removes all values that existed more than once within the original array.
  6. array_keys将丢弃数组的值部分,创建一个数组,其值是已定义的键。最后一步为您提供了一个结果,可以删除原始数组中多次存在的所有值。

#2


4  

You can filter them out using array_count_values():

您可以使用array_count_values()过滤掉它们:

$array = array('1','1','2','3');
$res = array_keys(array_filter(array_count_values($array), function($freq) {
    return $freq == 1;
}));

The function returns an array comprising the original values and their respective frequencies; you then pick only the single frequencies. The end result is obtained by retrieving the keys.

该函数返回一个包含原始值及其各自频率的数组;然后你只选择单个频率。通过检索密钥获得最终结果。

Demo

#3


2  

Try this code,

试试这个代码,

<?php
  $array = array('1','1','2','3');

 foreach($array as $data){
  $key= array_keys($array,$data);
   if(count($key)>1){

  foreach($key as $key2 => $data2){
    unset($array[$key2]);
      }
    }
   }
   $array=array_values($array);
   print_r($array);


?>

Output

    Array ( [0] => 2 [1] => 3 )

#4


2  

PHP offers so many array functions, you just have to combine them:

PHP提供了如此多的数组函数,您只需将它们组合在一起:

$arr = array_keys(array_filter(array_count_values($arr), function($val) {
    return $val === 1;
}));

Reference: array_keys, array_filter, array_count_values

参考:array_keys,array_filter,array_count_values

DEMO

#5


1  

Remove duplicate values from an array.

从数组中删除重复的值。

 array_unique($array)

 $array = array(4, "4", "3", 4, 3, "3");
 $result = array_unique($array);
 print_r($result);

 /*
   Array
   (
      [0] => 4
      [2] => 3
   )
  */

#1


4  

Depending on PHP version, this should work in all versions of PHP >= 4.0.6 as it doesn't require anonymous functions that require PHP >= 5.3:

根据PHP版本,这应该适用于所有版本的PHP> = 4.0.6,因为它不需要需要PHP> = 5.3的匿名函数:

function moreThanOne($val) {
    return $val < 2;
}


$a1 = array('1','1','2','3');
print_r(array_keys(array_filter(array_count_values($a1), 'moreThanOne')));

DEMO (Change the PHP version in the drop-down to select the version of PHP you are using)

DEMO(更改下拉列表中的PHP版本以选择您正在使用的PHP版本)

This works because:

这是因为:

  1. array_count_values will go through the array and create an index for each value and increment it each time it encounters it again.
  2. array_count_values将遍历数组并为每个值创建一个索引,并在每次遇到它时递增它。

  3. array_filter will take the created array and pass it through the moreThanOne function defined earlier, if it returns false, the key/value pair will be removed.
  4. array_filter将获取创建的数组并将其传递给之前定义的moreThanOne函数,如果返回false,则将删除键/值对。

  5. array_keys will discard the value portion of the array creating an array with the values being the keys that were defined. This final step gives you a result that removes all values that existed more than once within the original array.
  6. array_keys将丢弃数组的值部分,创建一个数组,其值是已定义的键。最后一步为您提供了一个结果,可以删除原始数组中多次存在的所有值。

#2


4  

You can filter them out using array_count_values():

您可以使用array_count_values()过滤掉它们:

$array = array('1','1','2','3');
$res = array_keys(array_filter(array_count_values($array), function($freq) {
    return $freq == 1;
}));

The function returns an array comprising the original values and their respective frequencies; you then pick only the single frequencies. The end result is obtained by retrieving the keys.

该函数返回一个包含原始值及其各自频率的数组;然后你只选择单个频率。通过检索密钥获得最终结果。

Demo

#3


2  

Try this code,

试试这个代码,

<?php
  $array = array('1','1','2','3');

 foreach($array as $data){
  $key= array_keys($array,$data);
   if(count($key)>1){

  foreach($key as $key2 => $data2){
    unset($array[$key2]);
      }
    }
   }
   $array=array_values($array);
   print_r($array);


?>

Output

    Array ( [0] => 2 [1] => 3 )

#4


2  

PHP offers so many array functions, you just have to combine them:

PHP提供了如此多的数组函数,您只需将它们组合在一起:

$arr = array_keys(array_filter(array_count_values($arr), function($val) {
    return $val === 1;
}));

Reference: array_keys, array_filter, array_count_values

参考:array_keys,array_filter,array_count_values

DEMO

#5


1  

Remove duplicate values from an array.

从数组中删除重复的值。

 array_unique($array)

 $array = array(4, "4", "3", 4, 3, "3");
 $result = array_unique($array);
 print_r($result);

 /*
   Array
   (
      [0] => 4
      [2] => 3
   )
  */