【问题标题】:How to parse semicolon-delimited lines of text in a csv file with fgetcsv()如何使用 fgetcsv() 解析 csv 文件中以分号分隔的文本行
【发布时间】:2016-08-17 09:54:28
【问题描述】:

我正在尝试读取 CSV 文件并将其转换为这样的数组。

$h = fopen("onderdelen-test.csv", "r");

echo '$parts = array(';

if($h) {
    while (($data = fgetcsv($h, 1000)) !== FALSE) {
        foreach ($data as $num) {
            $part = explode(';', "$num");

            echo "array('partid' => '$part[0]', ";
            echo "'descr' => '$part[1]'), ";
        }
    }
    fclose($h);
}

echo ')';

csv 看起来像这样

123456 ; partdescription
234567 ; partdescription, anotherdescription
345678 ; part, description and some other description

问题在于它还会在逗号上爆炸,而不仅仅是在分号上。 我尝试在描述周围添加引号,但确实在描述周围加上了一些我无法摆脱的奇怪问号。

编辑 1: 如果我在 fgetcsv 函数中使用分号作为分隔符,那么我无法通过键检索值,它只会在每次找到分号时启动另一个循环。

【问题讨论】:

  • php.net/manual/en/function.fgetcsv.php 第三个参数 - $delimiter
  • @ineersa 喜欢你杀死西方许多最快的枪支的方式:)
  • @ineersa 在底部添加了我对此的看法,我试过了,但每次都会开始一个新循环
  • PS:你不必将变量用双引号括起来$part = explode(';', $num);
  • PPS:您可以构建一个数组,然后 print_r($part); 并获得该数组的可视化表示,您不必像现在这样设计输出

标签: php csv text-parsing


【解决方案1】:

保持简单,因为您要做的就是先看看这个输入产生了什么,然后再继续做更大的事情

123456 ; partdescription
234567 ; partdescription, anotherdescription
345678 ; part, description and some other description

这段代码,注意我在fgetcsv中添加了第三个参数

<?php
$h = fopen("onderdelen-test.csv", "r");

if($h) {
    while (($data = fgetcsv($h, 1000, ';')) !== FALSE) {
        print_r($data);
        echo "partid = " . trim($data[0]) . "\n";
        echo "descr  = " . trim($data[1]) . "\n";
    }
    fclose($h);
}

产生这个输出

Array
(
    [0] => 123456
    [1] =>  partdescription
)
partid = 123456
descr =  partdescription
Array
(
    [0] => 234567
    [1] =>  partdescription, anotherdescription
)
partid = 234567
descr =  partdescription, anotherdescription
Array
(
    [0] => 345678
    [1] =>  part, description and some other description
)
partid = 345678
descr =  part, description and some other description

【讨论】:

  • 我尝试了所有我知道的foreach 可能性,但没有正确检索数据。你能帮我解决这个问题吗?
  • 我的意思是我需要遍历数组并执行一些查询但是当我使用普通的foreach 然后尝试 value[key] 我没有得到我需要的值
  • 对不起,我没有看到你的回声。这很好用,谢谢!我想我只是把它复杂化了。
【解决方案2】:

简单的sn-p解析csv文件:

    $i=0; $keys=array(); $output=array();
    $handle=fopen("onderdelen-test.csv", "r");
    if ($handle){
        while(($line = fgetcsv($handle,0,';')) !== false) {
            $i++;
            if ($i==1) {
                $keys=$line;
            } elseif ($i>1){
                $attr=array();
                foreach($line as $k=>$v){
                    $attr[trim($keys[$k])]=$v;
                }
                $output[]=$attr;
            }
        }
        fclose($handle);
    }

    //$output here is your data array

在这里,您将从 csv 文件中获取关联数组,其中包含文件第一行中的键。

    id ; description
123456 ; partdescription
234567 ; partdescription, anotherdescription
345678 ; part, description and some other description

结果数组:

Array
(
    [0] => Array
        (
            [id] => 123456 
            [description] =>  partdescription
        )

    [1] => Array
        (
            [id] => 234567 
            [description] =>  partdescription, anotherdescription
        )

    [2] => Array
        (
            [id] => 345678 
            [description] =>  part, description and some other description
        )

)

你的echo 有点不对劲。

【讨论】:

  • 你可能想取消它以使初学者更容易理解,特别是因为 OP 不使用类
  • @RiggsFolly 如果有任何改进,请随时编辑。删除了函数声明。
  • 这也会产生奇怪的输出。你可能想测试一下。它丢失了输入行之一
  • 或者您可能在第 1 行添加了标题行而您正在跳过它,对不起我的错误。虽然OP的输入文件没有标题行??!
  • @RiggsFolly 我试过了,但是输入真的很奇怪。我需要它是[0] =&gt; partid, [1] =&gt; descr
猜你喜欢
  • 2012-12-22
  • 2013-04-19
  • 2012-11-20
  • 2016-05-25
  • 2019-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多