cgee
cgee

Reputation: 1897

PHP array into a CSV file

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.

Upvotes: 0

Views: 82

Answers (2)

Seti
Seti

Reputation: 2314

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

Upvotes: 1

cgee
cgee

Reputation: 1897

fputcsv($fp, $fields);

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

Upvotes: 0

Related Questions