Excuse me ~

Source: Internet
Author: User
Please refer to the following link for more information ~~~~~~~~~~~~~~~~~~~~~~ The actual situation is that A and B have about 10 thousand data records. The result is c.txt. for details, see Section 333 ~~~~~~~~~~~~~~~~~~~~~~
The a.txt file is as follows:
123
222
333
444
555
666
777
888
999
000

The B .txt file is as follows:
123
222
444
666
000

If you do not use A data warehouse, removes B .txt data from a.txt. the actual situation is that A and B have approximately 10 thousand data records.
The result is c.txt. the content is as follows:
333
555
777
888
999


My idea is to read A and B to an array respectively, and then traverse A. if A row of data in A is not in B, the data is written to C.
I don't know if this idea is correct, but it is highly efficient.

------ Solution --------------------
Like you think, read the array and merge it.
Considering the large amount of data, you can read data separately. for example, two files read 100 lines each time. Then php has a special merging function, array_merge, which is very convenient.
------ Solution --------------------
No need to traverse.
Array_diff (array $ array1, array $ array2 [, array $...])
The returned array contains all values in the array of array1 but not in any other parameter array.

------ Solution --------------------
PHP code
Function w ($ cont, $ filename) {if (is_writable ($ filename) {if (! $ Handle = fopen ($ filename, 'A') {echo "cannot open the file $ filename"; exit;} if (fwrite ($ handle, $ cont) === FALSE) {echo "cannot be written to the file $ filename"; exit;} echo "successfully writes $ somecontent to the file $ filename"; fclose ($ handle );} else {echo "file $ filename cannot be written" ;}}$ cont_a = file_get_contents ("/path/filenameA"); $ arrA = explode ("\ n", $ cont_a ); $ cont_ B = file_get_contents ("/path/filenameB"); $ arrB = explode ("\ n", $ cont_ B); $ arrNewB = array_diff ($ arrB, $ arrA ); $ cont_newb = implode ("\ n", $ arrNewB); $ arrC = array_diff ($ arrA, $ arrB); $ cont_c = implode ("\ n", $ arrC ); w ($ cont_newb); w ($ cont_c );
------ Solution --------------------
It looks like the array_diff function + file function is enough. how is the efficiency of LZ.

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.