0

I've got some different CSV files which contain blocks of inputs separated by WR

I want to make four separate arrays with with the data.

Eg.

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

Would be come four arrays.

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;

The four arrays would then be inserted into four different MySQL tables.

I've managed to correctly split the headings into four arrays however I do not know how to then split each line of the data in the csv into separate arrays.

I hope I've made sense.

Thanks

4
  • What is the corresponding value under the WR column in your data rows? Is it blank or also WR? Commented Oct 4, 2012 at 11:34
  • I should also add, the WR blocks appear in different orders. Commented Oct 4, 2012 at 11:37
  • But for each CSV file, even if they appear in different orders, it will be consistent throughout the file? Like 5 Data Items, then WR, then 6 items then WR. and all data rows will follow that order? Commented Oct 4, 2012 at 11:39
  • Yes, it is consistent for the each file, however files have different orders. Commented Oct 4, 2012 at 11:44

4 Answers 4

1

If I understand correctly what you need, you can just use the explode method from php to split your strings from the csv file into an array. Using ; as delimiter :)

I hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

Use built in explode function like this:

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

?>

Comments

0

you can do this by exploding the array

$filename=explode(";",$string);

Comments

0

Based on what I understand from your question, this is the solution I came up with. First, get how the WR are ordered based on the headers, then generate an offset-length key-value pair. Use that key-value pair to slice the arrays of data which was exploded from each csv row.

<?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
    }
  }

?>

2 Comments

That looks like it may be what I was after.
I will include it into script and report back.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.