<?php/*==> 1.txt <==a:123b:1333c:333==> 2.txt <==a:3333aa:3433c:323dfa==> result.txt <==a:123:3333c:333:323dfa*/$file_1 = "1.txt";$file_2 = "2.txt";$f = fopen("$file_1", 'r') or die("Cann't Open the file.");while (!(feof($f))) { $line = explode(':', trim(fgets($f))); $f2 = fopen("$file_2", 'r') or die("Cann't Open the file."); while (!(feof($f2))) { $line2 = explode(':', trim(fgets($f2))); if ($line[0] == $line2[0]) { $line[] = $line2[1]; $aaaa = implode(":",$line); $output_file = fopen("./result.txt", 'a'); echo "$aaaa\n"; fwrite($output_file, "$aaaa\n"); fclose($output_file); } }}?>
如代碼所示,將1.txt和2.txt整理輸出到一個新文件result.txt, 效果如注釋部分。我寫的代碼處理的條數少的時候沒發(fā)現問題,當倆個文本都有十幾萬條記錄的時候,效率就出大事了,要整理10來個小時。初學PHP,求大師指點。
回復討論(解決方案)
$t = file('data/1.txt', FILE_IGNORE_NEW_LINES);foreach($t as $v) { list($k, $v) = explode(':', $v); $a[$k][] = $v;}$t = file('data/2.txt', FILE_IGNORE_NEW_LINES);foreach($t as $v) { list($k, $v) = explode(':', $v); $b[$k][] = $v;}foreach($a as $k=>$v) { if(isset($b[$k])) { file_put_contents('data/result.txt', join(':', array_merge(array($k), $v, $b[$k])). PHP_EOL, FILE_APPEND); }}
沒有嵌套的循環(huán),不會太慢的
$t = file('data/1.txt', FILE_IGNORE_NEW_LINES);foreach($t as $v) { list($k, $v) = explode(':', $v); $a[$k][] = $v;}$t = file('data/2.txt', FILE_IGNORE_NEW_LINES);foreach($t as $v) { list($k, $v) = explode(':', $v); $b[$k][] = $v;}foreach($a as $k=>$v) { if(isset($b[$k])) { file_put_contents('data/result.txt', join(':', array_merge(array($k), $v, $b[$k])). PHP_EOL, FILE_APPEND); }}
沒有嵌套的循環(huán),不會太慢的
1.txt?2.89M,?2.txt?3.12M
運行之后提示:
PHP?Fatal?error:??Allowed?memory?size?of?134217728?bytes?exhausted?(tried?to?allocate?32?bytes)
php.ini里設置的是memory_limit?=?128M,一下就用光了嗎?
會有這種事?文件并不大嘛。
可能是其他的原因
你把這兩個文件放到云盤上
直接設置?memory_limit?=?1024M,速度快多了,非常感謝@xuzuning。

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

??? ??











TOSECURELYHANDLEAUSTENCENDACTIONANDACTERIZINGINPHP, FORCUCTSESTEPS : 1. ALWAYSHASHPASSWORTHPASSWORD_HASH () ? VERVERIFYUSINGPANSWORD_VERIFY (), usePREPAREDSTATEMENTSTOPREVENTSQLINGERGED, andSTOREUSERSESSEATAIN $ _SESSIONSAFTERLOGIN.2.impleplempletrole ?? ACCESSC

PHP?? ?? ???? ???? ????? ??? ?? ??? ???? ?? ??? ??? ??? ???? ????. 1. finfo_file ()? ???? ?? ?? ??? ???? ???/jpeg? ?? ?? ?? ? ?????. 2. uniqid ()? ???? ??? ?? ??? ???? ? Web ?? ????? ??????. 3. php.ini ? html ??? ?? ?? ??? ???? ???? ??? 0755? ?????. 4. Clamav? ???? ???? ???? ??? ??????. ??? ??? ?? ???? ????? ???? ?? ??? ????? ???? ??? ? ??? ?????.

PHP?? ==? ==? ?? ???? ?? ??? ??????. == ?? ??? ?? ?? ?????. ?? ??, 5 == "5"? true? ????, ?? ??? ???? ?? ?? ??? ????? ????? (? : 5 === "5"? false? ?????. ?? ?????? ===? ? ???? ?? ?????? == ?? ??? ??? ???? ?????.

PHP?? ?? ??? ??? ???? ??? ??? ????. 1. ?? ??? ?? ? ?? ??? ??? ???? ???? ??? ? ????. ??? ??? ???? ????? ????? ???? ????. 2. ?? ?? ?? - ??, ??? ???? ?? ??? ?????. 3. ?? ???? ??? ??? ???? ??? ??? ?????. 4. Division? / ??? ???? 0?? ??? ?? ????? ??? ?? ??? ?? ? ? ????. 5. ???? ??? ???? ?? ?? ? ?? ??? ???? ? ??? ? ???, ??? ?? ? ? ??? ??? ???? ?????. ? ???? ???? ???? ??? ??? ??? ???? ?? ??? ? ??????? ????.

?, PHP? ?? ?? ?? ?????? ?? MongoDB ? Redis? ?? NOSQL ??????? ?? ??? ? ????. ?? MongoDBPHP ???? (PECL ?? Composer? ?? ??)? ???? ????? ????? ??? ?????? ? ???? ????? ??, ??, ?? ? ?? ??? ?????. ??, Predis ????? ?? Phpredis ??? ???? Redis? ???? ?? ? ?? ? ??? ???? ??? ????? Phpredis? ???? ?? Predis? ?? ??? ?????. ? ? ?? ??? ???? ? ????? ????.

tostaycurrentwithphpdevelopments ? bestpractices, followkeynewssources lifephp.netandphpweekly, adgytwithcommunitiesonforumsandconferences, readlingupdated andgrad indewfeatures, andreadorcontributetoopensourceproceprosts.first

phpbecamepupularforwebdevelopmentduetoiteofleneflening, whithhtml, wididepreadhostingsupport, andalargeecosystemincludingframeworkslikelaravelandcmsplatformsformslikewordpress.itexcelsinhandlingformsubmissions, managingussess, interptisussivers, ?? ???

TOSETTHERIGHTTIMEZONEINPHP, usedate_default_timezone_set () functionattStartOfyourscriptwitHavalidInlifiersuchas'America/new_york'.1.edate_default_timezone_set () beforeanydate/timeFunctions.2
