PHP 输出到 CSV

发布于 2024-10-22 17:30:30 字数 896 浏览 2 评论 0原文

我有以下 PHP 脚本,它从两个 CSV 文件中读取数据,目前我正在将数据放入表中,但我需要将其输出到 CSV 文件...

<?php

$products_file = fopen('Products.csv', 'r');
$manufacturers_file = fopen('Manufacturers.csv', 'r');

$manufacturers = array();

while (($manufacturers_line = fgetcsv($manufacturers_file)) !== FALSE) {

     $manufacturers[$manufacturers_line[0]] = $manufacturers_line [1];

    }

echo '<table><tr><th>SKU</th><th>Make and Model</th><th>Make and Model</th></tr>';

while (($products_line = fgetcsv($products_file)) !== FALSE ) {

  echo '<tr><td>'.$products_line[3].'</td><td>';

  echo $manufacturers[$products_line[5]];

  echo '</td><td>'.$products_line[4].'</td></tr>';
}

echo '</table>';

fclose($products_file);
fclose($manufacturers_file);

?>

如何使用 fputcsv 来执行此操作?

I have the following PHP script which reads from two CSV files, At the moment I am putting the data into a table however I need it to output to a CSV file...

<?php

$products_file = fopen('Products.csv', 'r');
$manufacturers_file = fopen('Manufacturers.csv', 'r');

$manufacturers = array();

while (($manufacturers_line = fgetcsv($manufacturers_file)) !== FALSE) {

     $manufacturers[$manufacturers_line[0]] = $manufacturers_line [1];

    }

echo '<table><tr><th>SKU</th><th>Make and Model</th><th>Make and Model</th></tr>';

while (($products_line = fgetcsv($products_file)) !== FALSE ) {

  echo '<tr><td>'.$products_line[3].'</td><td>';

  echo $manufacturers[$products_line[5]];

  echo '</td><td>'.$products_line[4].'</td></tr>';
}

echo '</table>';

fclose($products_file);
fclose($manufacturers_file);

?>

How can I do this using fputcsv ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缱绻入梦 2024-10-29 17:30:31

看起来您只需要更改它呈现 html 表的位置即可将其写入 csv 文件,如 PHP 文档中所示:-

$fp = fopen('newfile.csv', 'w');

while (($products_line = fgetcsv($products_file)) !== FALSE )
{
  fputcsv($fp, $products_line);
}

fclose($fp);

希望有所帮助。

瑞克.

Looks like you just need to change where it renders the html table to write it to a csv file like this as from the PHP documentation:-

$fp = fopen('newfile.csv', 'w');

while (($products_line = fgetcsv($products_file)) !== FALSE )
{
  fputcsv($fp, $products_line);
}

fclose($fp);

Hope that helps.

Rick.

掀纱窥君容 2024-10-29 17:30:31

您也可以这样做:

在 while 循环之外声明 $csv:

$csv = '';

然后在 while 循环中填充变量:

$csv .= $products_line[3].','.$manufacturers[$products_line[5]].','.$products_line[4]."\n";

然后一旦在循环之外,您可以将 $csv 写入文件:

$myFile = 'testFile.csv';
$fh = fopen($myFile, 'w') or die('cannot open file');
fwrite($fh, $csv);
fclose($fh);

完成!

注意 \n 两边的双引号。如果使用单引号,\n 字符将不会产生预期的结果。

You could also do it this way:

Declare $csv outside your while loop:

$csv = '';

Then in your while loop populate the variable:

$csv .= $products_line[3].','.$manufacturers[$products_line[5]].','.$products_line[4]."\n";

Then once outside your loop you can write $csv to a file:

$myFile = 'testFile.csv';
$fh = fopen($myFile, 'w') or die('cannot open file');
fwrite($fh, $csv);
fclose($fh);

Done!

Note the double quote around the \n. If you use single quotes the \n character will not have the expected result.

柒七 2024-10-29 17:30:31

我建议这样做:

$outfile = fopen('output.csv', 'w');

while (($products_line = fgetcsv($products_file)) !== FALSE 
      && fputcsv(
        array($products_line[3], $manufacturers[$products_line[5]], $products_line[4]
      ) !== FALSE) {
  echo '<tr><td>'.$products_line[3].'</td><td>';
  echo $manufacturers[$products_line[5]];
  echo '</td><td>'.$products_line[4].'</td></tr>';
}

首先,使用
创建一个新数组
数组($products_line[3], $manufacturers[$products_line[5]], $products_line[4]
然后这个数组被输入到fputcsv()

上面的代码同时输出 csv 和 html。如果您不需要 html 输出,请删除 echo

I'd suggest this:

$outfile = fopen('output.csv', 'w');

while (($products_line = fgetcsv($products_file)) !== FALSE 
      && fputcsv(
        array($products_line[3], $manufacturers[$products_line[5]], $products_line[4]
      ) !== FALSE) {
  echo '<tr><td>'.$products_line[3].'</td><td>';
  echo $manufacturers[$products_line[5]];
  echo '</td><td>'.$products_line[4].'</td></tr>';
}

First, a new array is being created with
array($products_line[3], $manufacturers[$products_line[5]], $products_line[4].
Then this array is being fed to fputcsv().

The above code outputs both csv and html. Remove the echo if you don't want html output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文