连接每个第n行的CSV文件

时间:2012-07-15 10:43:23

标签: php

考虑data.csv:

"1","2","3","4"
"5","6","7","8"
"9","10","11","12"
"13","14","15","16"
"17","18","19","20"
"21","22","23","24"
"25","26","27","28"
"29","30","31","32"
"33","34","35","36"

我需要阅读这个CSV并连接每一个,每隔三行说一次。所以期望的输出是:

array (
  [0] => 1,2,3,4,13,14,15,16,25,26,27,28    // row 1, 4 and 7
  [1] => 5,6,7,8,17,18,19,20,29,30,31,32    // row 2, 5 and 8
  [2] => 9,10,11,12,21,22,23,24,33,34,35,36 // row 3, 6 and 9
)

我需要n是变量的,所以它应该很容易,例如,连接每第4行:

array (
  [0] => 1,2,3,4,17,18,19,20,33,34,35,36    // row 1, 5 and 9
  [1] => 5,6,7,8,21,22,23,24                // row 2, 6
  [2] => 9,10,11,12,25,26,27,28             // row 3, 7
)

我现在有了这个:

$path = "data.csv";
$row = 1;
if (($handle = fopen($path, "r")) !== FALSE) {
  while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    for ($i = 1; $i <= 3; $i++) { // concatenate every 3rd row
      if ($row % $i == 0) $newrows[$i] .= implode(",", $data);
    }
    $row++;
  }
}

print_r($newrows);

但由于输出

,它无法按预期工作
Array (
  [1] => 1,2,3,45,6,7,89,10,11,1213,14,15,1617,18,19,2021,22,23,2425,26,27,2829,30,31,3233,34,35,36
  [2] => 5,6,7,813,14,15,1621,22,23,2429,30,31,32
  [3] => 9,10,11,1221,22,23,2433,34,35,36
)

你有更好的主意吗?这种数学逻辑总让我感到困惑! : - )

2 个答案:

答案 0 :(得分:4)

我现在无权访问我的服务器,因此我无法对其进行测试,但我认为您所拥有的简化版应该可以使用。

$path = "data.csv";
$row = 1;

$groupings = 4; // group every n rows

if(($handle = fopen($path, "r")) !== FALSE)
{
    while(($data = fgetcsv($handle, 1000, ",")) !== FALSE)
    {
        $where = $row % $groupings; // which grouping is this?
        $newrows[$where] .= implode(",", $data).",";

        $row++;
    }
}

for($i = 0; $i < count($newrows); $i++)
{
    $newrows[$i] = substr($newrows[$i], 0, -1);
}

你需要在初始while循环之后运行第二个循环,因为没有办法(我能想到)检测添加的数据是否是将要添加到该分组的最后一个数据。

答案 1 :(得分:3)

您可以迭代所有行,计算基于零的行号的余数(与SplFileObject的情况一样)作为新索引,然后用值填充array

在下面的考试中,$perEach数组将被填充:

$each = 3; # must be greater than 0
$path = '../data/numbers.csv'; # path to the csv file

$file = new SplFileObject($path);
$file->setFlags(SplFileObject::DROP_NEW_LINE);

$perEach = array();
foreach ($file as $lineNumber => $line)
{
    $index = $lineNumber % $each;
    $isNew = empty($perEach[$index]);
    if ($isNew) {
        $perEach[$index] = $line;
    } else {
        $perEach[$index] .= ',' . $line;
    }
}

您提供的数据的结果:

array(3) {
  [0] => string(55) ""1","2","3","4","13","14","15","16","25","26","27","28""
  [1] => string(55) ""5","6","7","8","17","18","19","20","29","30","31","32""
  [2] => string(58) ""9","10","11","12","21","22","23","24","33","34","35","36""
}

如果数据格式不一致并且您需要真正的CSV解析,SplFileObject也可以这样做。它可能更好,因为它允许首先初始化数组:

$file = new SplFileObject($path);
$file->setFlags(SplFileObject::READ_CSV);

$perEach = array_fill(0, $each, array());
foreach ($file as $lineNumber => $line)
{
    $index = $lineNumber % $each;
    $perEach[$index] = array_merge($perEach[$index], $line);
}

结果自然就是数组,以保持每行的值分开:

array(3) {
  [0]=>
  array(12) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
    [2]=>
    string(1) "3"
    [3]=>
    string(1) "4"
    [4]=>
    string(2) "13"
    [5]=>
    string(2) "14"
    [6]=>
    string(2) "15"
    [7]=>
    string(2) "16"
    [8]=>
    string(2) "25"
    [9]=>
    string(2) "26"
    [10]=>
    string(2) "27"
    [11]=>
    string(2) "28"
  }
  [1]=>
  array(12) {
    [0]=>
    string(1) "5"
    [1]=>
    string(1) "6"
    [2]=>
    string(1) "7"
    [3]=>
    string(1) "8"
    [4]=>
    string(2) "17"
    [5]=>
    string(2) "18"
    [6]=>
    string(2) "19"
    [7]=>
    string(2) "20"
    [8]=>
    string(2) "29"
    [9]=>
    string(2) "30"
    [10]=>
    string(2) "31"
    [11]=>
    string(2) "32"
  }
  [2]=>
  array(12) {
    [0]=>
    string(1) "9"
    [1]=>
    string(2) "10"
    [2]=>
    string(2) "11"
    [3]=>
    string(2) "12"
    [4]=>
    string(2) "21"
    [5]=>
    string(2) "22"
    [6]=>
    string(2) "23"
    [7]=>
    string(2) "24"
    [8]=>
    string(2) "33"
    [9]=>
    string(2) "34"
    [10]=>
    string(2) "35"
    [11]=>
    string(2) "36"
  }
}