Read through a file, and add numbers to array parts

牧云@^-^@ 提交于 2019-12-24 18:29:42

问题


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

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
)

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?


回答1:


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);
?>



回答2:


A smaller one (even if you already accepted another answer) but doesn't mean it's "better" (since it's not that easily readable). Still you can learn some tricks from it :

$path = "data.csv";
$datas = array_chunk(explode("\n",file_get_contents($path)),3);
array_walk($datas, create_function('&$v,$k', '$v = implode(\', \', $v);'));
var_dump($datas);

Way better than the previous one :

$path = "data.csv";  // path to the file
$datas = explode("\n",file_get_contents($path)); 
// $datas contains an array with each csv line as an array row
$finalArray = array(); // empty array we will fill
$datas = array_chunk($datas, 3); // http://fr.php.net/manual/en/function.array-chunk.php
foreach($datas as $data){
    $finalArray[] = implode(', ', $data);
}
var_dump($finalArray);

Previous one :

$path = "data.csv";  // path to the file
$row = 0; // initializing
$datas = explode("\n",file_get_contents($path)); 
// $datas contains an array with each csv line as an array row
$finalArray = array(); // empty array we will fill
// Let's loop $datas \o/
foreach($datas as $index => $data){ // 
    $finalArray[$row] = isset($finalArray[$row]) ? $finalArray[$row].', '.$data : $data; // filling the array
    if(($index+1)%3 == 0) $row++; // We jump from a row to another every 3 lines
}

var_dump($finalArray);



回答3:


You have to declare cell outside the cycle or it would be allways reset...

Here is the required code:

$path = "data.csv";
$row = 0;
$cell = 0;
if (($handle = fopen($path, "r")) !== FALSE) {
  while (($data = fgetcsv($handle, 1000, "\r\n")) !== FALSE) {
    $table[$row][$cell] = $data[0];
    $row += $cell == 2 ? 1 : 0; //If its last cell, increase row, else do nothing
    $cell = $cell == 2 ? 0 : $cell+1; //if its last cell, reset to 0, else add 1
  }
  fclose($handle);
}

I hope its easy to understand for you




回答4:


And here's my code :

//filter for empty lines
function remove_empty($e){
if(trim($e)!="") return true;
}

//reading the csv file and building the integer array
$arr = array_filter(explode("\r\n",file_get_contents("test.csv")),"remove_empty");
//new array initialization
$newArr = array();
//selecting 3 values in one loop
for($i=0;$i<count($arr);$i=$i+3){
    $newArr[] = implode(",",array($arr[$i],$arr[$i+1],$arr[$i+2]));
}

print_r($newArr);


来源:https://stackoverflow.com/questions/10420829/read-through-a-file-and-add-numbers-to-array-parts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!