Reads data from all rows in a CSV file at a time.
CopyCode The Code is as follows: <? PHP
$ File = fopen('windows_201511s.csv ', 'R ');
While ($ DATA = fgetcsv ($ file) {// read the contents of a row in the CSV each time.
// Print_r ($ data); // This is an array. To obtain each data, access the array subscript.
$ Goods_list [] = $ data;
}
// Print_r ($ goods_list );
/* Foreach ($ goods_list as $ ARR ){
If ($ arr [0]! = ""){
Echo $ arr [0]. "<br> ";
}
}*/
Echo $ goods_list [2] [0];
Fclose ($ file );
?>
Reads a row of data from a CSV file.
Copy codeThe Code is as follows: <? PHP
Function get_file_line ($ file_name, $ line ){
$ N = 0;
$ Handle = fopen ($ file_name, 'R ');
If ($ handle ){
While (! Feof ($ handle )){
+ + $ N;
$ Out = fgets ($ handle, 4096 );
If ($ line = $ n) break;
}
Fclose ($ handle );
}
If ($ line = $ n) return $ out;
Return false;
}
Echo get_file_line ("windows_201511s.csv", 10 );
?>
Number of rows (row interval) for reading CSV files
copy Code the code is as follows: function get_file_line ($ file_name, $ line_star, $ line_end) {
$ n = 0;
$ handle = fopen ($ file_name, "R");
if ($ handle) {
while (! Feof ($ handle) {
+ $ N;
$ out = fgets ($ handle, 4096 );
if ($ line_star <= $ n) {
$ Ling [] = $ out;
}< br> if ($ line_end = $ N) break;
}< br> fclose ($ handle);
}< br> if ($ line_end = $ n) return $ Ling;
return false;
}< br> $ AA = get_file_line ("windows_201511s.csv", 11, 20 ); // from row 11th to row 20th
foreach ($ AA as $ bb) {
echo $ BB. "
";
}< BR >?>
In addition, you can find two methods on the Internet (I don't know whether to enable them)
Copy codeThe Code is as follows: <?
$ Handle = fopen ("1.csv"," R ");
While (! Feof ($ handle )){
$ Buffer = fgetss ($ handle, 2048 );
$ DATA = explode (",", $ buffer );
$ Num = count ($ data );
For ($ I = 0; $ I <$ num; $ I ++ ){
Print_r ($ data );
}
}
?>
Copy codeThe Code is as follows: <?
$ Handle = fopen ("1.csv"," R ");
$ ROW = 1;
While ($ DATA = fgetcsv ($ handle, 1000 ,",")){
$ Num = count ($ data );
For ($ I = 0; $ I <$ num; $ I ++ ){
Echo $ data [$ I];
}
$ Row ++;
}
?>