[英]Concatenate every 3rd row to the previous 3rd row
I made an earlier post but did not formulate the issue correctly. 我发表了较早的文章,但未正确提出问题。 Hence, a question that may look like a previous one, but isn't.
因此,一个问题可能看起来像上一个问题,但并非如此。 Consider the following data.csv:
考虑以下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"
In reality, the rows and columns are much longer, but the principle stays the same. 实际上,行和列要长得多,但是原理保持不变。 The output should be as follows:
输出应如下所示:
The output then would be an array: 输出将是一个数组:
array (
[0] => 1,2,3,4,13,14,15,16,25,26,27,28
[1] => 5,6,7,8,17,18,19,20,29,30,31,32
[2] => 9,10,11,12,21,22,23,24,33,34,35,36
)
Thanks to @nickb I now have: 感谢@nickb,我现在有了:
$path = "data.csv";
$newrow = 1; $row = 1;
if (($handle = fopen($path, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if( !isset( $newrows[$newrow])) $newrows[$newrow] = '';
$newrows[$newrow] .= implode(",", $data);
if ($row % 3 == 0) {
$newrow++;
} else {
$newrows[$newrow] .= ',';
}
$row++;
}
}
print_r($newrows);
But this concatenated "all 3 consecutive" rows to eacht other, instead of "every third row". 但这将“所有3个连续的”行彼此串联在一起,而不是“每第三行”。 Who can tell me how to accomplish concatenating every 3rd row?
谁能告诉我如何完成每3行的连接? I tried this but it concatenates in a weird way:
我试过了,但是它以一种奇怪的方式连接:
$path = "data.csv";
$row = 1;
if (($handle = fopen($path, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
for ($i = 1; $i <= 3; $i++) {
if ($row % $i == 0) $newrows[$i] .= implode(",", $data);
}
$row++;
}
}
print_r($newrows);
Output: 输出:
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
)
edit
In reality, the csv is much larger and I need every 147th row to be concatenated to the previous 147th row, but the principle is the same I guess. edit
实际上,csv更大,我需要将每第147行连接到先前的第147行,但是我想原理是相同的。
EDIT: what you can do instead (and what is actually the smarter solution, shown here in Java) is: 编辑:您可以改为做的(实际上是更聪明的解决方案,在Java中显示在这里)是:
String[] newarray = new String[147];
for(int i = 0; i < yourarray.length; i++){
newarray[i%147].concat(yourarray[i]);
}
Replace String[] with whatever array type you need, and the concat() method with whatever method you use to concatenate arrays. 将String []替换为所需的任何数组类型,并将concat()方法替换为用于连接数组的任何方法。 The gist of the code is that you need some way to map the element in the original array at index 147n+C (for some non-negative integer n, and some integer C < 147) to the element in the new array at index C, which is essentially the purpose of modular arithmetic.
代码的要点是,您需要某种方式将原始数组中索引为147n + C的元素(对于某些非负整数n,且整数C <147)映射到索引为C的新数组中的元素,这实质上是模块化算术的目的。
Your general logic should be: 您的一般逻辑应为:
My PHP's too rusty to write the exact code to do that, but what you've got certainly won't do that. 我的PHP太生锈了,无法编写确切的代码来做到这一点,但是您肯定会做不到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.