* Using the generator to process CSV files
<?phpfunction getRows ($file) { $handle = fopen ($file, ' RB '); if ($handle = = = False) { throw new Exception (' Open file ' $file. ' error '); } while (feof ($handle) = = = False) { yield fgetcsv ($handle); } Fclose ($handle);} Memory is allocated only for one row in the CSV file//No need to read the entire CSV file to memory $generator = GetRows ('. /data/t_video.csv ');//foreach ($it as $row) {print_r ($row);} while ($generator->valid ()) { Print_r ($generator->current ()); $generator->next ();} $generator->rewind ();//http://php.net/manual/en/class.generator.php
Input:.. /data/t_video.csv
"9", "Other", "ULTIMATE PHOTO Guide", NULL, "Http://118.190.209.209/media/mp4/1.mp4", "http://118.190.209.209/media/png/ 1.png "," ULTIMATE PHOTO Guide "" 10 "," other "," the VERGE ", NULL," Http://118.190.209.209/media/mp4/2.mp4 ","/HTTP/ 118.190.209.209/media/png/2.the_verge.png "," The Verge "" 11 "," Other "," Microsoft Power BI ", NULL," http://118.190.209.209 /media/mp4/3.mp4 "," Http://118.190.209.209/media/png/3.png "," Microsoft Power BI "
Output
Array ( [0] = [1] = Dragon Education [2] = Dragon Education _ 3rd Issue [3] = NULL [4] =/http 118.190.209.209/media/mp4/%e9%be%99%e9%83%bd%e6%95%99%e8%82%b21103va0.mp4 [5] = http://118.190.209.209/ Media/poster/puyang_longdu_edu_1103.jpg [6] =-long education 3rd 20171103 Puyang First experimental primary school students Fan Ziheng) Array ( [0] = [1 ] [2] = Youth Mood-Puyang Fan County-Wang [3] = = NULL [4] = = Http://118.190.209.209/media/mp4/puyang/%E5 %88%98%e8%89%b3%e8%89%b31_2017_6_12_17_6_32_2f45_0.mp4 [5] = Http://118.190.209.209/media/poster/puyang /puyang01_liuyanyan_youth_emotion.jpg [6] = youth mood. Make a game: grab your finger)
Generator:
Generator implements Iterator {/ * Methods * public mixed current (void) public mixed Getreturn (void)
public mixed key (void) public void Next (void) public void Rewind (void) public mixed Send (mixed $v alue) Public mixed throw (Throwable $exception) publicly bool valid (void) public void __wakeup (void)
}}
Modern PHP uses the generator to process CSV files Generator