Strange array_unique Problems

Source: Internet
Author: User
Strange array_unique problem $ cardsn is a one-dimensional array, which contains the random number of member cards I generated. I want to use array_unique to de-duplicate and directly run the code: echo de-duplicate the number of elements in the array: count ($ cardsn); $ cardsnuarray_unique ($ cardsn); number of unique array elements after echobr deduplication:; count ($ cardsnu );

Strange array_unique problem $ cardsn is a one-dimensional array, which contains the random number of member cards I generated. I want to use array_unique to de-duplicate and directly run the code: echo "number of elements in the array before de-duplicating: "count ($ cardsn); $ cardsnu = array_unique ($ cardsn); echo" number of unique array elements after deduplication by br: "; count ($ cardsnu); guess the result is

Strange array_unique Problem
$ Cardsn is a one-dimensional array with a random number of member cards generated by me. I want to use array_unique to remove duplicates.
Directly run the Code:

Echo "number of elements in the array before deduplication:" count ($ cardsn );
$ Cardsnu = array_unique ($ cardsn );
Echo"
Number of unique array elements after deduplication: "; count ($ cardsnu );


Guess what the result is?

Number of elements before deduplication: 20000
Number of unique array elements after deduplication: 16384


Then trim the length of the original array $ cardsn to 17000 and run the command again. The result is as follows:

Number of elements before deduplication: 17000
Number of unique array elements after deduplication: 16384



Build the length of the original array to 16123 again. The execution result is as follows:

Number of elements before deduplication: 16123
Number of unique array elements after deduplication: 16123



After testing, it seems that the array_unique method can only process 16384 elements, no matter how many elements exist in the original array (as long as the length exceeds 16384 ), the maximum length of the unique array processed by array_unique is 16384.

------ Solution ----------------------

$arr = range(1,10000);
$arr1 = range(1, 18000);
$arr2 = array_merge($arr, $arr1);
$c = array_unique($arr2);
echo count($c); // 18000


This should not cause your problem. It is estimated that the data in your original array is like this.
------ Solution ----------------------
Reference:
I did this:


$ Str = "123456789ABCDEFGHJKLMNPQRSTUVWXYZ123456789 ";
$ Cardsn = array ();
$ Cardsnu = array ();
For ($ I = 0; I I <20000; $ I ++)
{
Array_push ($ cardsn, date ("y", time (). substr (str_shuffle ($ str), 0, 16 ));
}

Echo "number of elements in the array before deduplication:". count ($ cardsn );
$ Cardsnu = array_unique ($ cardsn );
Echo"
Number of unique array elements after deduplication: ". count ($ cardsnu );


The result is what I used to look like, miserable ....


Here is the execution:
Number of elements before deduplication: 20000
Number of unique array elements after deduplication: 20000

My account is php5.6.
------ Solution ----------------------
1. str_shuffle returns only result strings in disorder without changing the value of the original string. Therefore, the value returned by str_shuffle each time corresponds to the value returned by rand.
2, with [this article from the Internet (http://www.68idc.cn)] machine number generator (rand) is a pseudo random number sequence, its cycle is 32768 ,. That is to say, there will be no duplicates within 32768, and there will inevitably be duplicates.
3. I don't know what version of php you are using and why the pseudo-random number cycle is 16384.
4. If you want to exceed the limit of 32768, you need to write a pseudo-random number generator by yourself.
$rand= ($rand * $m + $c) % $maxrand;

Select the appropriate $ m and $ c to ensure that no duplicates are repeated within $ maxrand.

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.