通读文件,并添加号码组的部分 [英] Read through a file, and add numbers to array parts
本文介绍了通读文件,并添加号码组的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个CSV数以千计相互下面的数字文件。让我们通过使用此简化:
I have a CSV file with thousands of numbers underneath each other. Let's simplify by using this:
4
7
1
9
3
3
8
6
2
我要的是输出与每个键3个数字(用逗号崩盘)数组:
What I want is to output an array with 3 numbers per key (imploded by a comma):
array (
[0] => 4,7,1
[1] => 9,3,3
[2] => 8,6,2
)
我已经成功地得到这个,读取CSV:
I've managed to get to this, reading the CSV:
$path = "data.csv";
$row = 1;
if (($handle = fopen($path, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "\r\n")) !== FALSE) {
$cell = 0;
$table[$row][$cell] = $data[0];
$cell++;
}
fclose($handle);
}
我只是困惑,凡我怎么有最多$行和$细胞得到我想要的输出。你能帮忙吗?
I am just confused as to where an how I have to up $row and $cell to get the output I want. Could you help?
推荐答案
使用此,我测试和工程:
Use this, I tested and works:
<?php
$path = "data.csv";
$array = explode("\n", file_get_contents("data.csv"));
$numbers = array();
foreach(array_chunk($array, 3) as $number){
$numbers[] = implode(", ", $number);
}
print_r($numbers);
?>
这篇关于通读文件,并添加号码组的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文