0

I am trying to extract data from a CSV, run a function on some of it and spit it out into a new CSV. I'm getting stuck and writing the data to an array properly. Also, the file is created but its empty.

PHP

$file = fopen("yahrzeit-3.csv","r");

if ($file) {
  while ( ( $row = fgetcsv( $file, ";" ) ) !== false ) { // 3

  $data .= array ( $row[2], $row[3], $row[6], $row[7], $row[8], $row[14] );

}
print_r($data);
$fp = fopen('mailchimp.csv', 'w');

foreach ($data as $fields) {
    print $subfields;
    fputcsv($fp, $fields);
}
fclose( $fh ); // 5

}

fclose($fp);

2 Answers 2

1
$file = fopen("yahrzeit-3.csv","r");

if ($file) {
    $fp = fopen('mailchimp.csv', 'w');
    if (!$fp) {
        die('Cannot open output file');
    }
    while ( $row = fgetcsv( $file, ";" ) ) {
        $data = array ( $row[2], $row[3], $row[6], $row[7], $row[8], $row[14] );
        fputcsv($fp, $data);
    }
    fclose( $fp );
}
fclose($file);

is a lot more memory efficient than what you're trying to do

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

Comments

0
$data .= array ( $row[2], $row[3], $row[6], $row[7], $row[8], $row[14] );

should be

$data[] = array ( $row[2], $row[3], $row[6], $row[7], $row[8], $row[14] );

You are setting $data as a string and trying to cram an array into it.

Comments

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.