PHP str_getcsv数组问题 [英] php str_getcsv array issue

查看:114
本文介绍了PHP str_getcsv数组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上传CSV文件,然后使用str_getcsv解析它。除了我需要通过他们的方式来循环所有的伟大工程。理想情况下,它会是巨大的,有阵回头来看是这样的:

 阵列(
    [1] =>排列
       (
            [0] =>以CSV 1 //第一个ID
            [1] =>名称
            [2] =>地址
            [3] =>镇
            [4] =>州
            [5] =>压缩
            [6] =>电话
            [7] =>网站
            [8] =>其他
            [9] =>其他
        )
    [22] =>排列
       (
            [10] =>名称
            [11] =>地址
            [12] =>镇
            [13] =>州
            [14] =>压缩
            [15] =>电话
            [16] =>网站
            [17] =>其他
            [18] =>其他
        )
    [24] =>排列
       (
            [19] =>名称
            [20] =>地址
            [21] =>镇
            [22] =>州
            [23] =>压缩
            [24] =>电话
            [25] =>网站
            [26] =>其他
            [27] ​​=>其他
        )

然而,数据回来这样的:

 阵列

    [0] =>以CSV 1 //第一个ID
    [1] =>名称
    [2] =>地址
    [3] =>镇
    [4] =>州
    [5] =>压缩
    [6] =>电话
    [7] =>网站
    [8] =>其他
    [9] =>其他
22 // id字段
    [10] =>名称
    [11] =>地址
    [12] =>镇
    [13] =>州
    [14] =>压缩
    [15] =>电话
    [16] =>网站
    [17] =>其他
    [18] =>其他
24 // id字段
    [19] =>名称
    [20] =>地址
    [21] =>镇
    [22] =>州
    [23] =>压缩
    [24] =>电话
    [25] =>网站
    [26] =>其他
    [27] ​​=>其他

这是如何解决这一问题的任何建议看起来像在顶部的阵列?
现在我做的:

  $ csvfile =的file_get_contents($的TargetFile);
$ CSV = str_getcsv($ csvfile);


解决方案

str_getcsv()预计作为参数传递的字符串,的有一个的记录。结果
但由于源是一个文件反正最简单的方法可能是使用 fgetcsv()代替str_getcsv()

  $数据=阵列();
$计划生育=的fopen($的TargetFile,RB);
而(!的feof($ FP)){
    $数据[] = fgetcsv($ FP);
}
FCLOSE($ FP);


自足例如:

 < PHP
$的TargetFile ='soTest.csv';
设置($的TargetFile);$数据=阵列();
$计划生育=的fopen($的TargetFile,RB);
而(!的feof($ FP)){
    $数据[] = fgetcsv($ FP);
}
后续代码var_dump($的数据);
功能设置($的TargetFile){
    的file_put_contents($的TargetFile,<<< EOT
1,名称,地址,城市,州,邮编,电话,网站,其他,其他
2,姓名,地址,城市,州,邮编,电话,网站,其他,其他
3,姓名,地址,城市,州,邮编,电话,网站,其他,其他
EOT
    );
}

打印

 阵列(3){
  [0] =>
  阵列(10){
    [0] =>
    串(1)为1
    [1] =>
    串(4)名
    [2] =>
    字符串(7)地址
    [3] =>
    字符串(四)镇
    [4] =>
    串(5)国家
    [5] =>
    串(3)拉链
    [6] =>
    串(5)手机
    [7] =>
    字符串(7)网站
    [8] =>
    串(5)其他
    [9] =>
    串(5)其他
  }
  [1] =>
  阵列(10){
    [0] =>
    串(1)2
    [1] =>
    串(4)名
    [2] =>
    字符串(7)地址
    [3] =>
    字符串(四)镇
    [4] =>
    串(5)国家
    [5] =>
    串(3)拉链
    [6] =>
    串(5)手机
    [7] =>
    字符串(7)网站
    [8] =>
    串(5)其他
    [9] =>
    串(5)其他
  }
  [2] =>
  阵列(10){
    [0] =>
    串(1)3
    [1] =>
    串(4)名
    [2] =>
    字符串(7)地址
    [3] =>
    字符串(四)镇
    [4] =>
    串(5)国家
    [5] =>
    串(3)拉链
    [6] =>
    串(5)手机
    [7] =>
    字符串(7)网站
    [8] =>
    串(5)其他
    [9] =>
    串(5)其他
  }
}


EDIT2:对于使用的每个记录的第一元件作为结果阵列中的键:

 < PHP
$的TargetFile ='soTest.csv';
设置($的TargetFile);$数据=阵列();
$计划生育=的fopen($的TargetFile,RB);
而(!的feof($ FP)){
    $行= fgetcsv($ FP);
    $ ID = array_shift($行);
    $数据[$ ID] = $行;
}
后续代码var_dump($的数据);
功能设置($的TargetFile){
    的file_put_contents($的TargetFile,<<< EOT
1,名称,地址,城市,州,邮编,电话,网站,其他,其他
2,姓名,地址,城市,州,邮编,电话,网站,其他,其他
3,姓名,地址,城市,州,邮编,电话,网站,其他,其他
EOT
    );
}

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

However the data comes back like the following:

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

prints

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

这篇关于PHP str_getcsv数组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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