May I ask the great God, php how do arrays remove duplicate values?

Source: Internet
Author: User
Ask the great God, how does the PHP array remove duplicate values???
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
------Solution--------------------
Array value 3 also don't?
------Solution--------------------
Array_unique () Ah
------Solution--------------------
$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);
------Solution--------------------
$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
)

------Solution--------------------
References:
$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);
?,
------Solution--------------------
Reference:
$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.
------Solution--------------------
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))
)
);
Print_r ($t);

------Solution--------------------
$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.