0

my problem is, that I want to save an array into a CSV file. Here my code:

//Webseite auslesen
$string = file_get_contents("***Zensiert***");    
$helper = preg_match_all('!<a.*?href=\"([^\"]*)\"[^>]*>(.*?)</a>!', $string, $matches);    
$fl_array = preg_grep('/^http.*/', $matches[1]);
echo '<pre>';
print_r($fl_array);
echo '</pre>';

$fp = fopen('file.csv', 'a');

foreach ($fl_array as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);

The file is generated, but without content?! I didn't see the mistake...

Hope you can help me.

1
  • Attempt with the w+ permission on fopen(), secondly, what are the actual contents of $fl_array? I believe as a will place the file pointer at the end of the file. See docs: php.net/manual/en/function.fopen.php Commented May 2, 2014 at 11:04

2 Answers 2

1

I would change some lines to those

$fp = fopen('file.csv', 'w');
fputcsv($df, array_keys(reset($fl_array))); //Put column names as the 1st row (you may comment this line)
foreach ($fl_array as $fields) {
    fputcsv($fp, $fields);
}
fclose($fp);

This opens file to write.

And in your code you probably want to change that foreach into: fputcsv($fp, $fl_array );

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

Comments

0
fputcsv($fp, $fields);

$fields isn't an array.. so that was the problem.

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.