Welcome to the Linux community forum and interact with 2 million technical staff to access the contents of the csv file read by PHP. Code for reading data from all rows in the csv file at one time :? Php?filefopen(windows_2011_s.csv, r); while ($ datafgetcsv ($ file) {print_r reads a line of content in the CSV each time.
Welcome to the Linux community forum and interact with 2 million technical staff> go to PHP to read the contents of the csv file. Code for reading data from all rows in the csv file at one time :? Php $ file = fopen('windows_2011_s.csv ', 'R'); while ($ data = fgetcsv ($ file) {// read a line of content in the CSV each time // print_r
Welcome to the Linux community forum and interact with 2 million technicians>
PHP reads the contents of the csv file.
Reads data from all rows in a csv file at a time.
Code:
$ 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]."
";
}
}*/
Echo $ goods_list [2] [0];
Fclose ($ file );
?>
Reads a row of data from a csv file.
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 );
?>
Read csv files to specify the number of rows (row interval)
Code:
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;
}//
If ($ line_end = $ n) break;
}
Fclose ($ handle );
}
If ($ line_end = $ n) return $ ling;
Return false;
}
$ Aa = get_file_line ("windows_201511s.csv", 11, 20); // from 11th rows to 20th rows
Foreach ($ aa as $ bb ){
Echo $ bb ."
";
}
?>
In addition, you can find two methods on the Internet (I don't know whether to enable them)
[1] [2]