I know there is array_unique
function, but I want to remove duplicates. Is there a built-in function or do I have to roll my own.
我知道有array_unique函数,但我想删除重复项。是否有内置功能或我必须自己滚动。
Example input:
示例输入:
banna, banna, mango, mango, apple
Expected output:
预期产量:
apple
9 个解决方案
#1
12
You can use a combination of array_unique
, array_diff_assoc
and array_diff
:
您可以使用array_unique,array_diff_assoc和array_diff的组合:
array_diff($arr, array_diff_assoc($arr, array_unique($arr)))
#2
4
You can use
您可以使用
$singleOccurences = array_keys(
array_filter(
array_count_values(
array('banana', 'mango', 'banana', 'mango', 'apple' )
),
function($val) {
return $val === 1;
}
)
)
See
看到
-
array_count_values
— Counts all the values of an array - array_count_values - 计算数组的所有值
-
array_filter
— Filters elements of an array using a callback function - array_filter - 使用回调函数过滤数组的元素
-
array_keys
— Return all the keys or a subset of the keys of an array - array_keys - 返回数组的所有键或键的子集
- callbacks
- 回调
#3
2
Just write your own simple foreach loop:
只需编写自己的简单foreach循环:
$used = array();
$array = array("banna","banna","mango","mango","apple");
foreach($array as $arrayKey => $arrayValue){
if(isset($used[$arrayValue])){
unset($array[$used[$arrayValue]]);
unset($array[$arrayKey]);
}
$used[$arrayValue] = $arrayKey;
}
var_dump($array); // array(1) { [4]=> string(5) "apple" }
have fun :)
玩的开心 :)
#4
1
If you want to only leave values in the array that are already unique, rather than select one unique instance of each value, you will indeed have to roll your own. Built in functionality is just there to sanitise value sets, rather than filter.
如果您只希望在数组中保留已经唯一的值,而不是选择每个值的一个唯一实例,那么您确实必须自己滚动。内置功能可以清理价值集,而不是过滤。
#5
1
You want to remove any entries that have duplicates, so that you're left with only the entries that were unique in the list? Hmm it does sound like something you'll need to roll your own.
您想要删除任何具有重复项的条目,这样您只剩下列表中唯一的条目吗?嗯,它确实听起来像你需要自己滚动的东西。
#6
1
There is no existing function; You'll have to do this in two passes, one to count the unique values and one to extract the unique values:
没有现有的功能;您必须在两个过程中执行此操作,一个用于计算唯一值,另一个用于提取唯一值:
$count = array();
foreach ($values as $value) {
if (array_key_exists($value, $count))
++$count[$value];
else
$count[$value] = 1;
}
$unique = array();
foreach ($count as $value => $count) {
if ($count == 1)
$unique[] = $value;
}
#7
0
The answer on top looks great, but on a side note: if you ever want to eliminate duplicates but leave the first one, using array_flip twice would be a pretty simple way to do so. array_flip(array_flip(x))
顶部的答案看起来很棒,但是旁注:如果您想要消除重复但是留下第一个,使用array_flip两次将是一个非常简单的方法。 array_flip(array_flip(X))
#8
0
Only partially relevant to this specific question - but I created this function from Gumbo's answer for multi dimensional arrays:
只与这个特定问题部分相关 - 但我从Gumbo的多维数组答案中创建了这个函数:
function get_default($array)
{
$default = array_column($array, 'default', 'id');
$array = array_diff($default, array_diff_assoc($default, array_unique($default)));
return key($array);
}
In this example, I had cached statuses and each one other than the default was 0 (the default was 1). I index the default
array from the IDs, and then turn it into a string. So to be clear - the returned result of this is the ID of the default status providing it's in the same part of the multi dimensional array and not the key of it
在这个例子中,我有缓存状态,除了默认值之外的每个都是0(默认值是1)。我从ID索引默认数组,然后将其转换为字符串。所以要清楚 - 返回的结果是默认状态的ID,提供它在多维数组的同一部分而不是它的关键
#9
-1
PHP.net http://php.net/manual/en/function.array-unique.php
PHP.net http://php.net/manual/en/function.array-unique.php
array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
array array_unique(array $ array [,int $ sort_flags = SORT_STRING])
Takes an input array and returns a new array without duplicate values.
获取输入数组并返回没有重复值的新数组。
New solution:
新解决方案:
function remove_dupes(array $array){
$ret_array = array();
foreach($array as $key => $val){
if(count(array_keys($val) > 1){
continue;
} else {
$ret_array[$key] = $val;
}
}
#1
12
You can use a combination of array_unique
, array_diff_assoc
and array_diff
:
您可以使用array_unique,array_diff_assoc和array_diff的组合:
array_diff($arr, array_diff_assoc($arr, array_unique($arr)))
#2
4
You can use
您可以使用
$singleOccurences = array_keys(
array_filter(
array_count_values(
array('banana', 'mango', 'banana', 'mango', 'apple' )
),
function($val) {
return $val === 1;
}
)
)
See
看到
-
array_count_values
— Counts all the values of an array - array_count_values - 计算数组的所有值
-
array_filter
— Filters elements of an array using a callback function - array_filter - 使用回调函数过滤数组的元素
-
array_keys
— Return all the keys or a subset of the keys of an array - array_keys - 返回数组的所有键或键的子集
- callbacks
- 回调
#3
2
Just write your own simple foreach loop:
只需编写自己的简单foreach循环:
$used = array();
$array = array("banna","banna","mango","mango","apple");
foreach($array as $arrayKey => $arrayValue){
if(isset($used[$arrayValue])){
unset($array[$used[$arrayValue]]);
unset($array[$arrayKey]);
}
$used[$arrayValue] = $arrayKey;
}
var_dump($array); // array(1) { [4]=> string(5) "apple" }
have fun :)
玩的开心 :)
#4
1
If you want to only leave values in the array that are already unique, rather than select one unique instance of each value, you will indeed have to roll your own. Built in functionality is just there to sanitise value sets, rather than filter.
如果您只希望在数组中保留已经唯一的值,而不是选择每个值的一个唯一实例,那么您确实必须自己滚动。内置功能可以清理价值集,而不是过滤。
#5
1
You want to remove any entries that have duplicates, so that you're left with only the entries that were unique in the list? Hmm it does sound like something you'll need to roll your own.
您想要删除任何具有重复项的条目,这样您只剩下列表中唯一的条目吗?嗯,它确实听起来像你需要自己滚动的东西。
#6
1
There is no existing function; You'll have to do this in two passes, one to count the unique values and one to extract the unique values:
没有现有的功能;您必须在两个过程中执行此操作,一个用于计算唯一值,另一个用于提取唯一值:
$count = array();
foreach ($values as $value) {
if (array_key_exists($value, $count))
++$count[$value];
else
$count[$value] = 1;
}
$unique = array();
foreach ($count as $value => $count) {
if ($count == 1)
$unique[] = $value;
}
#7
0
The answer on top looks great, but on a side note: if you ever want to eliminate duplicates but leave the first one, using array_flip twice would be a pretty simple way to do so. array_flip(array_flip(x))
顶部的答案看起来很棒,但是旁注:如果您想要消除重复但是留下第一个,使用array_flip两次将是一个非常简单的方法。 array_flip(array_flip(X))
#8
0
Only partially relevant to this specific question - but I created this function from Gumbo's answer for multi dimensional arrays:
只与这个特定问题部分相关 - 但我从Gumbo的多维数组答案中创建了这个函数:
function get_default($array)
{
$default = array_column($array, 'default', 'id');
$array = array_diff($default, array_diff_assoc($default, array_unique($default)));
return key($array);
}
In this example, I had cached statuses and each one other than the default was 0 (the default was 1). I index the default
array from the IDs, and then turn it into a string. So to be clear - the returned result of this is the ID of the default status providing it's in the same part of the multi dimensional array and not the key of it
在这个例子中,我有缓存状态,除了默认值之外的每个都是0(默认值是1)。我从ID索引默认数组,然后将其转换为字符串。所以要清楚 - 返回的结果是默认状态的ID,提供它在多维数组的同一部分而不是它的关键
#9
-1
PHP.net http://php.net/manual/en/function.array-unique.php
PHP.net http://php.net/manual/en/function.array-unique.php
array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
array array_unique(array $ array [,int $ sort_flags = SORT_STRING])
Takes an input array and returns a new array without duplicate values.
获取输入数组并返回没有重复值的新数组。
New solution:
新解决方案:
function remove_dupes(array $array){
$ret_array = array();
foreach($array as $key => $val){
if(count(array_keys($val) > 1){
continue;
} else {
$ret_array[$key] = $val;
}
}