一个较小的(即使您已经接受了另一个答案),但并不意味着它“更好”(因为它不是那么容易阅读)。你仍然可以从中学到一些技巧:
$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);
比上一个好多了:
$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);
前一个 :
$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);