2016-03-29 22 views
0

私は基本的にcsvファイルから特定の行を取得する方法が不思議です(ファイルには5000行あり、2番目の列の値を取得します行3023~3311)。これらの列の値を取得すると、私はそれらをPHP配列に入れたいと思います。私がこれまで試したどのようなcsvファイルから特定の行を戻して配列に入れる方法

$search = $this->uri->segment(3); // get the row number  
$row1 = 0; 

    if (($handle = fopen("Folder/Data.csv", "r")) !== FALSE) 
    { 
     while (($data1 = fgetcsv($handle, 1000, ",")) !== FALSE) 
     { 
      $num1 = count($data1); // number of data rows 
      $row1++; 
      for ($c=0; $c < $num1; $c++) { 



       echo $data1[$c+1]; // to return second row 


      } 
     } 
     fclose($handle); 

しかし、唯一の1つの行を返し、私は周りに200を返し、配列にそれらを配置する必要があり、その

。どんな助けでも大歓迎です!

答えて

1

あなたはかなり近いです。 whileループ内でforループを実行する必要はありません。パラメータはあなたの基準に合わせて50の変更100に私の配列は、行の2列目からの値が含まれ、上記の例では

$file = fopen('/path/to/file', 'r'); 

// Helps us keep tabs on the row we are on 
$rowCount = 0; 
// Array that stores all of our column values. In this case, I'll be gathering 
// values from the second column 
$secondColumnArray = array(); 
while(false !== ($rowData = fgetcsv($file))){ 
    if($rowCount >= 50 && $rowCount <= 100){ 
     array_push($secondColumnArray, $rowData[1]); 
    } 
    $rowCount = $rowCount + 1; 
} 

print_r($secondColumnArray); 

fclose($file); 

:あなたはこのようなものを使用することができます。

関連する問題