How can I remove duplicate values from an array in PHP?
如何从PHP数组中删除重复值?
19 个解决方案
#1
197
Use array_unique().
使用array_unique()。
Example:
例子:
$array = array(1, 2, 2, 3);
$array = array_unique($array); // Array is now (1, 2, 3)
#2
21
//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 )
#3
8
Use array_values(array_unique($array));
使用元素(array_unique(数组)美元);
array_unique
: for unique array array_values
: for reindexing
array_unique:用于惟一数组array_values:用于转换。
#4
4
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 */
#5
4
The only thing which worked for me is:
唯一对我有用的是:
$array = array_unique($array, SORT_REGULAR);
#6
2
explode(",", implode(",", array_unique(explode(",", $YOUR_ARRAY))));
爆炸(”、“内爆”、“,array_unique(爆炸(”、“YOUR_ARRAY美元))));
This will take care of key associations and serialize the keys for the resulting new array :-)
这将处理关键的关联,并序列化生成的新数组的键:-)
#7
2
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);
#8
2
$result = array();
foreach ($array as $key => $value){
if(!in_array($value, $result))
$result[$key]=$value;
}
#9
1
That's a great way to do it. Might want to make sure its output is back an array again. Now you're only showing the last unique value.
这是一个伟大的方式去做。可能希望确保其输出又返回一个数组。现在你只显示最后一个独特的价值。
Try this:
试试这个:
$arrDuplicate = array ("","",1,3,"",5);
foreach (array_unique($arrDuplicate) as $v){
if($v != "") { $arrRemoved[] = $v; }
}
print_r ($arrRemoved);
#10
1
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更快。
#11
1
if (@!in_array($classified->category,$arr)){
$arr[] = $classified->category;
?>
<?php } endwhile; wp_reset_query(); ?>
first time check value in array and found same value ignore it
第一次检查数组中的值并发现相同的值忽略它
#12
1
Remove duplicate values from an associative array in PHP.
从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]);
}
Array ( [0] => aaa-aaa [1] => 12/1/1 [2] => 1.15 )
阵列([0]= > aaa-aaa[1]= > 12/1/1[2]= > 1.15)
#13
0
$arrDuplicate = array ("","",1,3,"",5);
foreach(array_unique($arrDuplicate) as $v){
if($v != "" ){$arrRemoved = $v; }}
print_r($arrRemoved);
#14
0
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] = $val;
}
}
return $retArray;
}
#15
0
try this short & sweet code -
试试这个简短甜蜜的密码-
$array = array (1,4,2,1,7,4,9,7,5,9);
$unique = array();
foreach($array as $v){
isset($k[$v]) || ($k[$v]=1) && $unique[] = $v;
}
var_dump($unique);
Output -
输出-
array(6) {
[0]=>
int(1)
[1]=>
int(4)
[2]=>
int(2)
[3]=>
int(7)
[4]=>
int(9)
[5]=>
int(5)
}
#16
0
it can be done through function i made three function duplicate returns the values which are duplicate in array .second function single return only those values which are single mean not repeated in array and third and full function return all values but not duplicated if any value is duplicated it convert it to single
可以通过我做了三个函数复制函数返回值重复的数组,接着单只返回的值是单身的意思是不重复的数组和第三,函数返回值但不重复任何值复制它转换是单身
function duplicate($arr){
$duplicate;
$count=array_count_values($arr);
foreach($arr as $key=>$value){
if($count[$value]>1){
$duplicate[$value]=$value;
}
}
return $duplicate;
}
function single($arr){
$single;
$count=array_count_values($arr);
foreach($arr as $key=>$value){
if($count[$value]==1){
$single[$value]=$value;
}
}
return $single;
}
function full($arr,$arry){
$full=$arr+$arry;
sort($full);
return $full;
}
}
}
#17
0
<?php
$arr1 = [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 = $arr[0];
$uni_arr[] = $arr[0];
for($i=0; $i<count($arr);$i++){
if($curr != $arr[$i]) {
$uni_arr[] = $arr[$i];
$curr = $arr[$i];
}
}
return $uni_arr;
}
#18
0
There can be multiple ways to do these, which are as follows
可以有多种方法来完成这些任务,如下所示。
//first method
$filter = array_map("unserialize", array_unique(array_map("serialize", $arr)));
//second method
$array = array_unique($arr, SORT_REGULAR);
#19
-1
I have done this without using any function.
我没有使用任何函数。
$arr = array("1", "2", "3", "4", "5", "4", "2", "1");
$len = count($arr);
for ($i = 0; $i < $len; $i++) {
$temp = $arr[$i];
$j = $i;
for ($k = 0; $k < $len; $k++) {
if ($k != $j) {
if ($temp == $arr[$k]) {
echo $temp."<br>";
$arr[$k]=" ";
}
}
}
}
for ($i = 0; $i < $len; $i++) {
echo $arr[$i] . " <br><br>";
}
#1
197
Use array_unique().
使用array_unique()。
Example:
例子:
$array = array(1, 2, 2, 3);
$array = array_unique($array); // Array is now (1, 2, 3)
#2
21
//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 )
#3
8
Use array_values(array_unique($array));
使用元素(array_unique(数组)美元);
array_unique
: for unique array array_values
: for reindexing
array_unique:用于惟一数组array_values:用于转换。
#4
4
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 */
#5
4
The only thing which worked for me is:
唯一对我有用的是:
$array = array_unique($array, SORT_REGULAR);
#6
2
explode(",", implode(",", array_unique(explode(",", $YOUR_ARRAY))));
爆炸(”、“内爆”、“,array_unique(爆炸(”、“YOUR_ARRAY美元))));
This will take care of key associations and serialize the keys for the resulting new array :-)
这将处理关键的关联,并序列化生成的新数组的键:-)
#7
2
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);
#8
2
$result = array();
foreach ($array as $key => $value){
if(!in_array($value, $result))
$result[$key]=$value;
}
#9
1
That's a great way to do it. Might want to make sure its output is back an array again. Now you're only showing the last unique value.
这是一个伟大的方式去做。可能希望确保其输出又返回一个数组。现在你只显示最后一个独特的价值。
Try this:
试试这个:
$arrDuplicate = array ("","",1,3,"",5);
foreach (array_unique($arrDuplicate) as $v){
if($v != "") { $arrRemoved[] = $v; }
}
print_r ($arrRemoved);
#10
1
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更快。
#11
1
if (@!in_array($classified->category,$arr)){
$arr[] = $classified->category;
?>
<?php } endwhile; wp_reset_query(); ?>
first time check value in array and found same value ignore it
第一次检查数组中的值并发现相同的值忽略它
#12
1
Remove duplicate values from an associative array in PHP.
从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]);
}
Array ( [0] => aaa-aaa [1] => 12/1/1 [2] => 1.15 )
阵列([0]= > aaa-aaa[1]= > 12/1/1[2]= > 1.15)
#13
0
$arrDuplicate = array ("","",1,3,"",5);
foreach(array_unique($arrDuplicate) as $v){
if($v != "" ){$arrRemoved = $v; }}
print_r($arrRemoved);
#14
0
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] = $val;
}
}
return $retArray;
}
#15
0
try this short & sweet code -
试试这个简短甜蜜的密码-
$array = array (1,4,2,1,7,4,9,7,5,9);
$unique = array();
foreach($array as $v){
isset($k[$v]) || ($k[$v]=1) && $unique[] = $v;
}
var_dump($unique);
Output -
输出-
array(6) {
[0]=>
int(1)
[1]=>
int(4)
[2]=>
int(2)
[3]=>
int(7)
[4]=>
int(9)
[5]=>
int(5)
}
#16
0
it can be done through function i made three function duplicate returns the values which are duplicate in array .second function single return only those values which are single mean not repeated in array and third and full function return all values but not duplicated if any value is duplicated it convert it to single
可以通过我做了三个函数复制函数返回值重复的数组,接着单只返回的值是单身的意思是不重复的数组和第三,函数返回值但不重复任何值复制它转换是单身
function duplicate($arr){
$duplicate;
$count=array_count_values($arr);
foreach($arr as $key=>$value){
if($count[$value]>1){
$duplicate[$value]=$value;
}
}
return $duplicate;
}
function single($arr){
$single;
$count=array_count_values($arr);
foreach($arr as $key=>$value){
if($count[$value]==1){
$single[$value]=$value;
}
}
return $single;
}
function full($arr,$arry){
$full=$arr+$arry;
sort($full);
return $full;
}
}
}
#17
0
<?php
$arr1 = [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 = $arr[0];
$uni_arr[] = $arr[0];
for($i=0; $i<count($arr);$i++){
if($curr != $arr[$i]) {
$uni_arr[] = $arr[$i];
$curr = $arr[$i];
}
}
return $uni_arr;
}
#18
0
There can be multiple ways to do these, which are as follows
可以有多种方法来完成这些任务,如下所示。
//first method
$filter = array_map("unserialize", array_unique(array_map("serialize", $arr)));
//second method
$array = array_unique($arr, SORT_REGULAR);
#19
-1
I have done this without using any function.
我没有使用任何函数。
$arr = array("1", "2", "3", "4", "5", "4", "2", "1");
$len = count($arr);
for ($i = 0; $i < $len; $i++) {
$temp = $arr[$i];
$j = $i;
for ($k = 0; $k < $len; $k++) {
if ($k != $j) {
if ($temp == $arr[$k]) {
echo $temp."<br>";
$arr[$k]=" ";
}
}
}
}
for ($i = 0; $i < $len; $i++) {
echo $arr[$i] . " <br><br>";
}