5

考虑 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 并每隔 n 次连接一次,比如每隔 3 行连接一次。所以想要的输出是:

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
)

你有更好的主意吗?这个数理逻辑一直让我不解!:-)

4

2 回答 2

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 循环之后运行第二个循环,因为没有办法(我能想到)检测要添加的数据是否是要添加到该分组中的最后一个数据。

于 2012-07-15T10:54:02.793 回答
3

您可以遍历所有行,计算从零开始的行号的余数(就像 的情况一样SplFileObject)作为新索引,然后array用该值填充 an 。

在以下示例中,$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"
  }
}
于 2012-07-15T11:31:40.403 回答