Ask the great God, how does the PHP array remove duplicate values?

Source: Internet
Author: User
Example

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

After the process of processing, once you know and repeat the data, delete,

Finally I need an array, because 3 repeats


Reply to discussion (solution)

Array value 3 also don't?

Array value 3 also don't?


Oh, yes

Array_unique () Ah

$arr = Array (1,2,3,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,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 the idea, but the code seems to be simple: $arr = Array (1,2,3,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 I thought.

$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 Cow B

Just a little bit easier.

$array = Array (1,2,3,3), $t = Array_diff ($array,  Array_keys (    array_diff (      array_count_values ($array), Array (1))    )  ;p rint_r ($t);

$array =array (1,2,3,3);
$a =array_count_values ($array);
$item =array ();
foreach ($a as $k = $v) if ($v ==1) $item []= $k;
Print_r ($item);

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.