PHP array of millions of data to eliminate data deduplication implementation Code _php tutorial

Source: Internet
Author: User
If you get a list of UID, the number is more than million lines, the format is as follows:
Copy CodeThe code is as follows:
10001000
10001001
10001002
......
10001000
......
10001111

In fact, using the characteristics of the PHP array, very good for the row weight, we first look at the definition of PHP array: The array in PHP is actually an ordered map. A map is a type that associates values to the keys. This type is optimized in many ways, so it can be used as a real array, or as a list (vector), a hash (an implementation of a mapping), a dictionary, a collection, a stack, a queue, and more possibilities. The value of an array element can also be another array. Tree structures and multidimensional arrays are also allowed.

In an array of PHP, the keys are also known as indexes, which are unique, and we can use this feature for the weight of the sample code as follows:
Copy CodeThe code is as follows:
Defines an array for storing the result after the row
$result = Array ();
Read UID list file
$fp = fopen (' test.txt ', ' R ');

while (!feof ($FP))
{
$uid = fgets ($FP);
$uid = Trim ($uid);
$uid = Trim ($uid, "\ r");
$uid = Trim ($uid, "\ n");

if ($uid = = ")
{
Continue
}
Use UID as key to see if the value exists
if (Empty ($result [$uid])
{
$result [$uid] = 1;
}
}

Fclose ($FP);

Save the results to a file
$content = ";
foreach ($result as $k = $v)
{
$content. = $k. " \ n ";
}
$fp = fopen (' Result.txt ', ' W ');
Fwrite ($fp, $content);
Fclose ($FP);
?>

More than 20 lines of code, you can be more than millions of data for the row weight, efficiency is good, very practical. Mobile phone number, email, you can also use this method for weight.

Also, this method can also be used for two files for the work of the weight, if you have two UID list file, the format and the above UID list, the sample program is as follows:
Copy CodeThe code is as follows:
Defines an array for storing the results after a row
$result = Array ();
Read the first UID list file and put $result_1
$fp = fopen (' test_1.txt ', ' R ');
while (!feof ($FP))
{
$uid = fgets ($FP);
$uid = Trim ($uid);
$uid = Trim ($uid, "\ r");
$uid = Trim ($uid, "\ n");
if ($uid = = ")
{
Continue
}
Write $result with UID key, overwrite if duplicate
$result [$uid] = 1;
}
Fclose ($FP);
Read the second UID list file and perform a sort of a load operation
$fp = fopen (' test_2.txt ', ' R ');
while (!feof ($FP))
{
$uid = fgets ($FP);
$uid = Trim ($uid);
$uid = Trim ($uid, "\ r");
$uid = Trim ($uid, "\ n");
if ($uid = = ")
{
Continue
}
Use UID as key to see if the value exists
if (Empty ($result [$uid])
{
$result [$uid] = 1;
}
}
Fclose ($FP);
$result saved in the following the result of the weight can be output to the file, code omitted
?>

When you think about it, it's easy to see that using this feature of the array can also solve more problems in our work.

http://www.bkjia.com/PHPjc/321981.html www.bkjia.com true http://www.bkjia.com/PHPjc/321981.html techarticle If you get a list of UID, the number is more than million lines, the format is as follows: Copy code code as follows: 10001000 10001001 10001002 ... 10001000 ... 10001111 actually using PHP arrays of ...

  • Related Article

    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.