如何使用PHP将列添加到CSV [英] How to add columns to CSV using PHP

查看:197
本文介绍了如何使用PHP将列添加到CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将列添加到现有的csv文件中,但是我找不到解决该问题的任何方法.我使用"\ t"和chr(9)创建列,但未成功,因此请通过提供帮助我如果有谁可以给​​我合适的解决方案

I need to add columns to an existing csv file ,but i can't find any solution to the problem.I have used "\t" and chr(9) to create columns but no success so please help me by providing me the right solution if any one can

推荐答案

尝试一下,看看 fgetcsv() fputcsv() a>在手册中

Try this, and have a look at fgetcsv() and fputcsv() in the manual

<?php
$newCsvData = array();
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $data[] = 'New Column';
        $newCsvData[] = $data;
    }
    fclose($handle);
}

$handle = fopen('test.csv', 'w');

foreach ($newCsvData as $line) {
   fputcsv($handle, $line);
}

fclose($handle);

?> 

这篇关于如何使用PHP将列添加到CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆