php str_getcsv 数组问题

2022-01-09 00:00:00 csv arrays parsing upload php

我正在上传一个 csv 文件,然后使用 str_getcsv 对其进行解析.一切都很好,只是我需要一种方法来循环它们.理想情况下,让数组返回并看起来像这样会很棒:

I am uploading a csv file and then parsing it using str_getcsv. All works great except that I need a way to cycle through them. Ideally, it'd be great to have the array come back and look like this:

Array (      
    [1] => Array
       (
            [0] => 1 // first id in csv
            [1] => name
            [2] => address
            [3] => town
            [4] => state
            [5] => zip
            [6] => phone
            [7] => website
            [8] => other
            [9] => other
        )
    [22] => Array
       (
            [10] => name
            [11] => address
            [12] => town
            [13] => state
            [14] => zip
            [15] => phone
            [16] => website
            [17] => other
            [18] => other
        )
    [24] => Array
       (
            [19] => name
            [20] => address
            [21] => town
            [22] => state
            [23] => zip
            [24] => phone
            [25] => website
            [26] => other
            [27] => other
        )
)

但是数据返回如下:

Array
(
    [0] => 1 // first id in csv
    [1] => name
    [2] => address
    [3] => town
    [4] => state
    [5] => zip
    [6] => phone
    [7] => website
    [8] => other
    [9] => other
22 // id field
    [10] => name
    [11] => address
    [12] => town
    [13] => state
    [14] => zip
    [15] => phone
    [16] => website
    [17] => other
    [18] => other
24// id field
    [19] => name
    [20] => address
    [21] => town
    [22] => state
    [23] => zip
    [24] => phone
    [25] => website
    [26] => other
    [27] => other

关于如何解决这个问题,让它看起来像顶部的数组有什么建议吗?现在我正在做:

Any suggestions on how to fix this to look like the array at the top? right now I'm doing:

$csvfile = file_get_contents($targetFile);
$csv = str_getcsv($csvfile);

推荐答案

str_getcsv() 需要字符串作为参数传递为 一个 记录.
但是由于您的源无论如何都是一个文件,最简单的方法可能是使用 fgetcsv() 而不是 str_getcsv()

str_getcsv() expects the string passed as parameter to be one record.
But since your source is a file anyway the easiest way is probably to use fgetcsv() instead of str_getcsv()

$data = array();
$fp = fopen($targetFile, 'rb');
while(!feof($fp)) {
    $data[] = fgetcsv($fp);
}
fclose($fp);

<小时>

独立示例:


self-contained example:

<?php
$targetFile = 'soTest.csv';
setup($targetFile);

$data = array();
$fp = fopen($targetFile, 'rb');
while(!feof($fp)) {
    $data[] = fgetcsv($fp);
}
var_dump($data);


function setup($targetFile) {
    file_put_contents($targetFile, <<< eot
1,name,address,town,state,zip,phone,website,other,other
2,name,address,town,state,zip,phone,website,other,other
3,name,address,town,state,zip,phone,website,other,other
eot
    );
}

打印

array(3) {
  [0]=>
  array(10) {
    [0]=>
    string(1) "1"
    [1]=>
    string(4) "name"
    [2]=>
    string(7) "address"
    [3]=>
    string(4) "town"
    [4]=>
    string(5) "state"
    [5]=>
    string(3) "zip"
    [6]=>
    string(5) "phone"
    [7]=>
    string(7) "website"
    [8]=>
    string(5) "other"
    [9]=>
    string(5) "other"
  }
  [1]=>
  array(10) {
    [0]=>
    string(1) "2"
    [1]=>
    string(4) "name"
    [2]=>
    string(7) "address"
    [3]=>
    string(4) "town"
    [4]=>
    string(5) "state"
    [5]=>
    string(3) "zip"
    [6]=>
    string(5) "phone"
    [7]=>
    string(7) "website"
    [8]=>
    string(5) "other"
    [9]=>
    string(5) "other"
  }
  [2]=>
  array(10) {
    [0]=>
    string(1) "3"
    [1]=>
    string(4) "name"
    [2]=>
    string(7) "address"
    [3]=>
    string(4) "town"
    [4]=>
    string(5) "state"
    [5]=>
    string(3) "zip"
    [6]=>
    string(5) "phone"
    [7]=>
    string(7) "website"
    [8]=>
    string(5) "other"
    [9]=>
    string(5) "other"
  }
}

<小时>

edit2:使用每条记录的第一个元素作为结果数组中的键:


edit2: For using the first element of each record as the key in the result array:

<?php
$targetFile = 'soTest.csv';
setup($targetFile);

$data = array();
$fp = fopen($targetFile, 'rb');
while(!feof($fp)) {
    $row = fgetcsv($fp);
    $id = array_shift($row);
    $data[$id] = $row;
}
var_dump($data);


function setup($targetFile) {
    file_put_contents($targetFile, <<< eot
1,name,address,town,state,zip,phone,website,other,other
2,name,address,town,state,zip,phone,website,other,other
3,name,address,town,state,zip,phone,website,other,other
eot
    );
}

相关文章