php 如何使用zend导入CSV

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6334354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:03:41  来源:igfitidea点击:

how to import CSV using zend

phpzend-frameworkcsv

提问by JABD

How do I import CSV files using zend framework? Should I use zend_file_transfer or is there any special class that I have to look into? Also if I use zend_file_transfer is there any special validator for CSV?

如何使用 zend 框架导入 CSV 文件?我应该使用 zend_file_transfer 还是我必须研究任何特殊的类?另外,如果我使用 zend_file_transfer 是否有任何特殊的 CSV 验证器?

回答by Kenrick Buchanan

you don't have to use any zend libraries to import csv files, you can just use native php functions, take a look at fgetcsv

你不必使用任何 zend 库来导入 csv 文件,你可以使用原生的 php 函数,看看fgetcsv

$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}

回答by Maghiel Dijksman

You could also use SplFileObjectfor reading CSV files.

您还可以SplFileObject用于读取 CSV 文件。

From the php manual:

从 php 手册:

<?php
    $file = new SplFileObject("animals.csv");
    $file->setFlags(SplFileObject::READ_CSV);
    foreach ($file as $row) {
        list($animal, $class, $legs) = $row;
        printf("A %s is a %s with %d legs\n", $animal, $class, $legs);
    }
?> 

http://php.net/manual/en/splfileobject.fgetcsv.php

http://php.net/manual/en/splfileobject.fgetcsv.php

回答by faken

There is currently no way to do this with the Zend Framework. How can one be sure?

Zend 框架目前无法做到这一点。怎么能确定呢?

For example, Zend_Translatesupports translation with CSV files, but if you check the the source code of the respective adapter (Zend_Translate_Adapter_Csv), you can verify it uses fgetcsv, and not a specific Zend class. Besides, this CSV adapter comes with the following warning:

例如,Zend_Translate支持使用 CSV 文件进行翻译,但是如果您检查相应适配器的源代码(Zend_Translate_Adapter_Csv),您可以验证它使用的是fgetcsv,而不是特定的 Zend 类。此外,此 CSV 适配器带有以下警告:

Note: Beware that the Csv Adapter has problems when your Csv files are encoded differently than the locale setting of your environment. This is due to a Bug of PHP itself which will not be fixed before PHP 6.0 (http://bugs.php.net/bug.php?id=38471). So you should be aware that the Csv Adapter due to PHP restrictions is not locale aware.

注意:当您的 Csv 文件的编码方式与您环境的区域设置不同时,请注意 Csv 适配器会出现问题。这是由于 PHP 本身的一个 Bug,在 PHP 6.0 (http://bugs.php.net/bug.php?id=38471) 之前不会修复。所以你应该知道,由于 PHP 限制,Csv Adapter 不支持区域设置。

which is related with the problems of the fgetcsvfunction.

这与fgetcsv函数的问题有关。

回答by Brad Lucas

Here's a function that reads a csv file and returns an array of items that contain the first two column data values.

这是一个读取 csv 文件并返回包含前两列数据值的项目数组的函数。

This function could read a file of first_name,last_name for example.

例如,此函数可以读取 first_name,last_name 的文件。

function processFile ($filename) {
    $rtn = array();

    if (($handle = fopen($filename, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $item = array();
            $item[] = $data[0];
            $item[] = $data[1];
            $rtn[] = $item;
        }
    }
    return $rtn;
 }