0

WRで区切られた入力のブロックを含むいくつかの異なるCSVファイルがあります

データを使用して4つの別々の配列を作成したいと思います。

例えば。

Datum;Uhrzeit;WR;Status;SolIrr;TmpMod;TmpAmb;Wind;DaySumIrr;WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac;WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac;WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac

4つのアレイが来るでしょう。

WR;Status;SolIrr;TmpMod;TmpAmb;Wind;DaySumIrr;
WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac;
WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac;
WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac;

次に、4つの配列が4つの異なるMySQLテーブルに挿入されます。

見出しを4つの配列に正しく分割できましたが、csv内のデータの各行を別々の配列に分割する方法がわかりません。

私は理にかなっていると思います。

ありがとう

4

4 に答える 4

1

必要なものを正しく理解していれば、php の爆発メソッドを使用して、文字列を csv ファイルから配列に分割できます。を使用します。区切り文字として:)

これが役立つことを願っています。

于 2012-10-04T11:32:35.773 に答える
0

あなたの質問から私が理解したことに基づいて、これは私が思いついた解決策です。まず、ヘッダーに基づいてWRがどのように順序付けられているかを取得してから、オフセット長のキーと値のペアを生成します。そのキーと値のペアを使用して、各csv行から展開されたデータの配列をスライスします。

<?php

  $headers = "Datum;Uhrzeit;WR;Status;SolIrr;TmpMod;TmpAmb;Wind;DaySumIrr;WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac;WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac;WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac";
  $headers_arr = explode(";", $headers);

  // track where WR is positioned in the array
  $WR_offset = array();
  foreach ($headers_arr as $i => $value) {
    if ($value == "WR") { $WR_offset[] = $i; }
  }
  $WR_offset[] = count($headers_arr); // assume imaginary WR as last header

  // loop through the WR_offset array,
  // to get the WR position, and the number of fields before the next WR position
  // to be used in array_slice
  for ($i = 0; $i < count($WR_offset) - 1; $i++) {
    $offset = $WR_offset[$i] + 1; // 
    $length = $WR_offset[$i+1] - $WR_offset[$i] - 1;
    // store the offset and length as key-value pair
    $slice_params[$offset] = $length;
  }

  // assuming $lines contains the CSV data rows, and $line is a single CSV data row
  foreach ($lines as $line) {
    $row_array = explode(";", $line);
    // loop through the generated offset-length value pair
    foreach ($slice_params as $offset => $length) {
      $array_chunks = array_slice($row_array, $offset, $length);
      // do what you want to do with the chunk here
    }
  }

?>
于 2012-10-04T12:08:40.360 に答える
0

次のような組み込みexplode関数を使用します。

<?php

$string = 'WR;Status;SolIrr;TmpMod;TmpAmb;Wind;DaySumIrr;
WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac;
WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac;
WR;Pac;DaySum;Status;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Temp;Uac;';

# convert into lines
$lines = explode( PHP_EOL, $string );

# convert into items
$items = array();
foreach ( $lines as $line ) {
    $items[] = explode( ';', $line );
}

?>
于 2012-10-04T11:37:08.343 に答える
0

exploding配列でこれを行うことができます

$filename=explode(";",$string);
于 2012-10-04T11:37:58.307 に答える