PHP read the CSV file content Instance Code _php tutorial

Source: Internet
Author: User

Read data from all rows in a CSV file at once
< span="">

$file = fopen (' windows_2011_s.csv ', ' R ');
while ($data = Fgetcsv ($file)) {//reads a row of CSV each time
Print_r ($data); This is an array, to get every data, access the array subscript can
$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
<>< span="">

function Get_file_line ($file _name, $line) {
$n = 0;
$handle = fopen ($file _name, ' R ');
if ($handle) {
while (!feof ($handle)) {
+ + $n; Www.jbxue.com
$out = Fgets ($handle, 4096);
if ($line = = $n) break;
}
Fclose ($handle);
}
if ($line = = $n) return $out;
return false;
}
Echo get_file_line ("Windows_2011_s.csv", 10);
?>

Read CSV file number of rows (line interval)
<>< span="">

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_2011_s.csv", 11, 20); From line 11th to line 20th
foreach ($aa as $BB) {
echo $BB. "
";
}
?>

In addition to find two methods from the Internet (no test, do not know good or bad to make)
<>< span="">

$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);
}
}
?>

Code 2,

$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 + +;
}
?>

http://www.bkjia.com/PHPjc/440198.html www.bkjia.com true http://www.bkjia.com/PHPjc/440198.html techarticle reads the data of all rows in the CSV file at once $file = fopen (' windows_2011_s.csv ', ' R '), while ($data = Fgetcsv ($file)) {//reads a row of the CSV each time//print _r ($data); This ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.