繁体   English   中英

如何使用 PHP 解析每行都用方括号括起来的 CSV?

[英]How do I parse a CSV where each row is enclosed in square brackets using PHP?

我在尝试传递从伦敦交通局收到的提要时偶然发现了 CSV 格式。 使用 PHP 解析它的最佳方法是什么?

http://countdown.api.tfl.gov.uk/interfaces/ura/instant_V1?circle=51.49288,-0.147425,100&ReturnList=StopPointName,EstimatedTime,StopID

[4,"1.0",1418639278611]
[1,"Eccleston Bridge","35312",1418639472000]
[1,"Eccleston Bridge","35312",1418639395000]
[1,"Eccleston Bridge","35312",1418639397000]
[1,"Eccleston Bridge","35312",1418639696000]
[1,"Eccleston Bridge","35312",1418639742000]
[1,"Eccleston Bridge","35312",1418639731000]
[1,"Eccleston Bridge","35312",1418640051000]
[1,"Eccleston Bridge","35312",1418639938000]
...

谢谢。

由于每一行单独看起来像JSON,你可以使用json_decode()的位置:

// input as string
$input = '....';

// split by lines
$lines = explode( "\n", $input );

// walk through results
for( $i=0; $i<count($lines); $i++ ) {

  // parse row
  $row = json_decode( $lines[i] );

  // do something with it ...

}

将每一行解析为单独的字符串后,您可以使用substr()来修剪括号。

例如:

echo substr("1418639472000]",0,-1);
> 1418639472000

echo substr("[1234",1);
> 1234

这将为您提供每行的关联数组

$file = file('http://countdown.api.tfl.gov.uk/interfaces/ura/instant_V1?circle=51.49288,-0.147425,100&ReturnList=StopPointName,EstimatedTime,StopID');

array_walk($file, function(&$line) {
    $line = json_decode($line);
    if (count($line) == 3) {
        $line = array_combine(array('id', 'count', 'timestamp'), $line);
    } else {
        $line = array_combine(array('id', 'station_name', 'station_id', 'timestamp'), $line);
    }
});

print_r($file);

您不需要对json_decode()进行迭代调用。 因为严格格式化的字符串几乎是有效的 json,所以您只需要用逗号替换换行符,然后将单个字符串包裹在方括号中,然后json_decode()就准备好吞噬字符串并吐出索引数组的索引数组。 请注意,在某些系统上,您可能需要替换\\r\\n而不仅仅是\\n

代码:(演示

$bracedCSV = <<<CSV
[4,"1.0",1418639278611]
[1,"Eccleston Bridge","35312",1418639472000]
[1,"Eccleston Bridge","35312",1418639395000]
[1,"Eccleston Bridge","35312",1418639397000]
[1,"Eccleston Bridge","35312",1418639696000]
[1,"Eccleston Bridge","35312",1418639742000]
[1,"Eccleston Bridge","35312",1418639731000]
[1,"Eccleston Bridge","35312",1418640051000]
[1,"Eccleston Bridge","35312",1418639938000]
CSV;

var_export(
    json_decode('[' . str_replace("\n", ',', $bracedCSV) . ']')
);

输出:

array (
  0 => 
  array (
    0 => 4,
    1 => '1.0',
    2 => 1418639278611,
  ),
  1 => 
  array (
    0 => 1,
    1 => 'Eccleston Bridge',
    2 => '35312',
    3 => 1418639472000,
  ),
  2 => 
  array (
    0 => 1,
    1 => 'Eccleston Bridge',
    2 => '35312',
    3 => 1418639395000,
  ),
  3 => 
  array (
    0 => 1,
    1 => 'Eccleston Bridge',
    2 => '35312',
    3 => 1418639397000,
  ),
  4 => 
  array (
    0 => 1,
    1 => 'Eccleston Bridge',
    2 => '35312',
    3 => 1418639696000,
  ),
  5 => 
  array (
    0 => 1,
    1 => 'Eccleston Bridge',
    2 => '35312',
    3 => 1418639742000,
  ),
  6 => 
  array (
    0 => 1,
    1 => 'Eccleston Bridge',
    2 => '35312',
    3 => 1418639731000,
  ),
  7 => 
  array (
    0 => 1,
    1 => 'Eccleston Bridge',
    2 => '35312',
    3 => 1418640051000,
  ),
  8 => 
  array (
    0 => 1,
    1 => 'Eccleston Bridge',
    2 => '35312',
    3 => 1418639938000,
  ),
)

暂无
暂无

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

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