简体   繁体   English

如何使用 zend 导入 CSV

[英]how to import CSV using zend

How do I import CSV files using zend framework?如何使用 zend 框架导入 CSV 文件? Should I use zend_file_transfer or is there any special class that I have to look into?我应该使用 zend_file_transfer 还是需要查看任何特殊的 class ? Also if I use zend_file_transfer is there any special validator for CSV?此外,如果我使用 zend_file_transfer 是否有任何用于 CSV 的特殊验证器?

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

You could also use SplFileObject for 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

There is currently no way to do this with the Zend Framework. Zend 框架目前无法做到这一点。 How can one be sure?怎么能确定呢?

For example, Zend_Translate supports 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.例如, Zend_Translate支持使用 CSV 文件进行翻译,但是如果您检查相应适配器的源代码 ( Zend_Translate_Adapter_Csv ),您可以验证它使用fgetcsv ,而不是特定的 Zend class。 Besides, this CSV adapter comes with the following warning:此外,这款 CSV 适配器带有以下警告:

Note: Beware that the Csv Adapter has problems when your Csv files are encoded differently than the locale setting of your environment.注意:当您的 Csv 文件的编码与您环境的区域设置不同时,请注意 Csv 适配器会出现问题。 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).这是由于 PHP 本身的错误导致的,在 PHP 6.0 之前无法修复(http://bugs.php.net/bug.php?id=3)。 So you should be aware that the Csv Adapter due to PHP restrictions is not locale aware.因此,您应该知道 Csv 适配器由于 PHP 限制而无法识别区域设置。

which is related with the problems of the fgetcsv function.这与fgetcsv function 的问题有关。

Here's a function that reads a csv file and returns an array of items that contain the first two column data values.这是一个 function 读取 csv 文件并返回包含前两列数据值的项目数组。

This function could read a file of first_name,last_name for example.例如,这个 function 可以读取 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;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM