2012-05-02 10 views
1

私は数千の数字が互いに重なっているCSVファイルを持っています。のは、これを使用して簡素化してみましょう:ファイルを読み込み、配列部分に数値を追加する

4 
7 
1 
9 
3 
3 
8 
6 
2 

私が欲しいのは、出力に(カンマで爆縮)のキーごとに3つの数字の配列である:

array (
    [0] => 4,7,1 
    [1] => 9,3,3 
    [2] => 8,6,2 
) 

私が読んで、これを取得するために管理してきました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); 
} 

は、私がどのように私は私が欲しいの出力を取得するまでの$行と$セルを持っている場所に同じくらい混乱しています。手伝ってくれる?

+2

理由だけで、 '( "\ nを"、のfile_get_contents( "data.csv")を)爆発ではありませんか;'? –

+4

カンマ区切りの値がないと考えると、実際にはCSVではありません。通常のファイルとして扱うほうが少し速いかもしれません。 –

+1

代わりに['file()'](http://php.net/file)と['array_chunk()'](http://php.net/array_chunk)を使って、 '' array_map ](http://php.net/array_map)(またはその他の方法) – salathe

答えて

3

使用これは、私がテストされ、動作します:

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

これは素晴らしいです、ありがとう! – Pr0no

+0

私は自分のlocalhostで作成しました@hime –

+0

私と同じではありませんo_O – Shikiryu

1

あなたはサイクルの外にセルを宣言するために持っているか、それがオールウェイズリセットされるだろう...

ここでは、必要なコードです:

$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); 
} 

私はあなた

+0

これはうまくいきません、fgetcsvは区切り文字として1文字しか使用できません。 – ehime

+0

ファイルを読み込むために働いたと仮定して、元のコードを編集しました。それが実際には機能しないと思うなら、答えではなく質問にコメントしてください。 – Kyborek

2

のために理解して簡単に願っていますあなたがすでに別の回答を受け入れたとしても、それより小さいものですが、それは「より良い」という意味ではありません。それでもあなたはそれからいくつかのトリックを学ぶことができます:

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

前1:

$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); 
1

そしてここでは、私のコードです:

//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); 
関連する問題