Home  >  Article  >  Backend Development  >  PHP file operation-add data from other files to this file

PHP file operation-add data from other files to this file

不言
不言Original
2018-04-04 14:43:551529browse

The content of this article is php file operation-add the data of other files to this file, now share it with everyone, friends in need can refer to it

GitHub source code
In fact, our The program writes answers based on the following questions:

There are two text files A.txt B.txt
A.txt 30 million lines, userid is unique, userid and username are separated by spaces, as shown below:
userid username
1 yi yi
2 er er
3 san
…  …
B.txt 30 million lines, userid is unique, userid and realname are separated by spaces, as shown below:
userid realname
1                                                                                                                                                                                                                                   ##userid realname
1 Go to the third column of B.txt and give the time complexity.

In our program, by default, the row data of the two files are in one-to-one correspondence, that is, the nth row of data in A corresponds to the nth row of data in B. In this way, the time complexity of our program is O(n).

But in actual operation, there will definitely be a situation where the data lines of the two files do not correspond one to one. In this case, I only think of the simplest operation with a time complexity of O(n^2) , I don’t know if there is a better algorithm to solve this problem.

If the file is read out and constructed into a data structure whose query is O(1) or O(logn), the complexity should become O(n) or O(nlogn), but when encountering a file Too large, how to deal with the situation when the constructed data structure is too large, learn from the practice of database b-tree index?

<?php
header("content-type:text/html;charset=utf-8");

function decodeLine(string $lineData, string $delimiter = null)
{    if (is_null($delimiter)) {        $delimiter = &#39; &#39;;
    }    return explode($delimiter, $lineData);
}

function encodeLine(array $dataList, string $delimiter = null)
{    if (is_null($delimiter)) {        $delimiter = &#39; &#39;;
    }    return implode($delimiter, $dataList);
}$testA = fopen(&#39;./TestData/FileOperation/testA.txt&#39;, &#39;r&#39;);$testB = fopen(&#39;./TestData/FileOperation/testB.txt&#39;, &#39;r+&#39;);$tmpFile = tmpfile();//while (($bBuffer = fgets($testB)) != false) {    $bList = decodeLine(trim($bBuffer, "\n\r"));    $tmpList = $bList;    if (($aBuffer = fgets($testA)) != false) {        $aList = decodeLine(trim($aBuffer, "\n\r"));        if ($aList[0] == $bList[0]) {            $strEncoding = mb_detect_encoding($aList[1], [&#39;ASCII&#39;, &#39;UTF-8&#39;, &#39;GB2312&#39;]);            $resStr = mb_convert_encoding($aList[1], &#39;UTF-8&#39;, $strEncoding);
            array_push($tmpList, $resStr."\n");
        }
    }    fwrite($tmpFile, encodeLine($tmpList));
}

rewind($tmpFile);
rewind($testB);while (!feof($tmpFile)) {    $tmpBuffer = fread($tmpFile, 1024);    fwrite($testB, $tmpBuffer);
}fclose($tmpFile);fclose($testA);fclose($testB);

       

GitHub Source Code

In fact, our program is based on the following issues Written answer:

There are two text files A.txt B.txt
A.txt 30 million lines, userid is unique, userid and username are separated by spaces, as follows:

userid username

1 yi yi
2 er
3 san
…  …
B.txt 30 million lines, userid is unique, userid and realname are separated by spaces, as shown below:
userid realname
1 一
2  二
3   三
…  …
Please write a piece of code to find the username corresponding to the userid in B.txt in A.txt and fill it in B.txt The third column gives the time complexity.

In our program, by default, the row data of the two files are in one-to-one correspondence, that is, the nth row of data in A corresponds to the nth row of data in B. In this way, the time complexity of our program is O(n).

But in actual operation, there will definitely be a situation where the data lines of the two files do not correspond one to one. In this case, I only think of the simplest operation with a time complexity of O(n^2) , I don’t know if there is a better algorithm to solve this problem.

If the file is read out and constructed into a data structure whose query is O(1) or O(logn), the complexity should become O(n) or O(nlogn), but when encountering a file Too large, how to deal with the situation when the constructed data structure is too large, learn from the practice of database b-tree index?

<?php
header("content-type:text/html;charset=utf-8");

function decodeLine(string $lineData, string $delimiter = null)
{    if (is_null($delimiter)) {        $delimiter = &#39; &#39;;
    }    return explode($delimiter, $lineData);
}

function encodeLine(array $dataList, string $delimiter = null)
{    if (is_null($delimiter)) {        $delimiter = &#39; &#39;;
    }    return implode($delimiter, $dataList);
}$testA = fopen(&#39;./TestData/FileOperation/testA.txt&#39;, &#39;r&#39;);$testB = fopen(&#39;./TestData/FileOperation/testB.txt&#39;, &#39;r+&#39;);$tmpFile = tmpfile();//while (($bBuffer = fgets($testB)) != false) {    $bList = decodeLine(trim($bBuffer, "\n\r"));    $tmpList = $bList;    if (($aBuffer = fgets($testA)) != false) {        $aList = decodeLine(trim($aBuffer, "\n\r"));        if ($aList[0] == $bList[0]) {            $strEncoding = mb_detect_encoding($aList[1], [&#39;ASCII&#39;, &#39;UTF-8&#39;, &#39;GB2312&#39;]);            $resStr = mb_convert_encoding($aList[1], &#39;UTF-8&#39;, $strEncoding);
            array_push($tmpList, $resStr."\n");
        }
    }    fwrite($tmpFile, encodeLine($tmpList));
}

rewind($tmpFile);
rewind($testB);while (!feof($tmpFile)) {    $tmpBuffer = fread($tmpFile, 1024);    fwrite($testB, $tmpBuffer);
}fclose($tmpFile);fclose($testA);fclose($testB);

Related recommendations:

Get the directory for PHP file operations Files and methods of calculating relative paths

patch files PHP file operations to implement code sharing

The above is the detailed content of PHP file operation-add data from other files to this file. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn