1

大きなCSVファイルがあります。最初の列には、プロセッサの名前が含まれています。2つ目は、プロセッサのベンチマークスコアです。例えば:

Intel Pentium Dual T3400 @ 2.16GHz、1322

PHPスクリプトを使用して、最初の列で文字列(E. Pentium Dual T3400など)を検索し、(検索ごとに結果が1つしかない場合は、エラーメッセージを返します)、の値を含む変数を作成します。 2番目の列。

これが役立つかどうかはわかりませんが、次のように表示されることを期待していました。

$cpuscore = csvsearch(CSV_file,query_string,column#_to_search,column#_to_return)

$ cpuscoreには、検索クエリに一致するプロセッサ名のスコアが含まれます。

同様の結果が得られるものを自由に提案してください。MySQLを持っていますが、CSVからテーブルをインポートする権限がありません。

4

2 に答える 2

1

PHP 関数 fgetcsv()、http://php.net/manual/en/function.fgetcsv.php を使用して csv ファイルを行ごとにトラバースできます。例えば:

$ch = fopen($path_to_file, "r");
$found = '';

/* If your csv file's first row contains Column Description you can use this to remove the first row in the while */
$header_row = fgetcsv($ch);

/* This will loop through all the rows until it reaches the end */
while(($row = fgetcsv($ch)) !== FALSE) {

    /* $row is an array of columns from that row starting at 0 */
    $first_column = $row[0];

    /* Here you can do your search */
    /* If found $found = $row[1]; */
    /* Now $found will contain the 2nd column value (if found) */

}
于 2012-10-16T20:30:14.647 に答える
0

csv ファイルの各行を反復処理して、探している単語を見つけ、そこから結果をコンパイルするのが好きです。ここにあなたが始めるための何かがあります:

     <?php
   $query = "Intel Core i7 3600M"     
   $file = file('db.csv');    
       foreach($file as $value) { 
         if(stristr($value,$query)){
            $items = explode(",", $value); echo $items[1];
              }; 
         };
    ?>
于 2012-10-16T20:29:34.990 に答える