How can I remove duplicate values from the php array? Example
$ Array = array (1, 2, 3)
After being processed by the program, once the data is known and duplicated, it will be deleted,
In the final result, I need array (1, 2) because 3 already exists.
Reply to discussion (solution)
Is the array value 3 not required?
Is the array value 3 not required?
Yes
Array_unique ()
$ Arr = array (1, 2, 3 );
$ Str = implode ('', $ arr );
$ Arrres = array_unique ($ arr );
Foreach ($ arrres as $ k => $ v)
{
$ Num = substr_count ($ str, $ v );
If ($ num = 1)
{
$ Res [] = $ v;
}
}
Print_r ($ res );
$array = array(1,2,3,3);$t = array_diff($array, array_keys( array_filter( array_count_values($array), function($v) { return $v>1;}) ) );print_r($t);
Array
(
[0] => 1
[1] => 2
)
$ Arr = array (1, 2, 3 );
$ Str = implode ('', $ arr );
$ Arrres = array_unique ($ arr );
Foreach ($ arrres as $ k => $ v)
{
$ Num = substr_count ($ str, $ v );
If ($ num = 1)
{
$ Res [] = $ v;
}
}
Print_r ($ res );
Thank you for your ideas, but the code seems to be simple: $ Arr = array (1, 2, 3 );
$ Str = implode ('', $ arr );
Foreach ($ arr as $ k => $ v)
{
$ Num = substr_count ($ str, $ v );
If ($ num = 1 ){
$ Res [] = $ v;
}
}
Print_r ($ res );
?>,
$array = array(1,2,3,3);$t = array_diff($array, array_keys( array_filter( array_count_values($array), function($v) { return $v>1;}) ) );print_r($t);
Array
(
[0] => 1
[1] => 2
)
Just like what I think
$array = array(1,2,3,3);$t = array_diff($array, array_keys( array_filter( array_count_values($array), function($v) { return $v>1;}) ) );print_r($t);
Array
(
[0] => 1
[1] => 2
)
Moderator Niu B
A little simpler
$array = array(1,2,3,3);$t = array_diff($array, array_keys( array_diff( array_count_values($array), array(1)) ) );print_r($t);
$ Array = array (1, 2, 3 );
$ A = array_count_values ($ array );
$ Item = array ();
Foreach ($ a as $ k = >$ v) if ($ v = 1) $ item [] = $ k;
Print_r ($ item );