Reputation: 19
I'm creating a csv file through a PHP script that goes through the results of a query.
The rows I get when I do:
foreach $result as $row
look like this:
$row
array (
'id' => '9',
'description' => 'a nice book',
'product-title' => 'Book title',
'references' => '4044782308578',
'id_bookstore' => 2,
)
So far so good. My problem is that 'id_bookstore' I ask the database to do some operations, not to include it in the csv file.
But if I don't include 'id_bookstore' in the csv file in fputcsv($file, $row, ';');
I get an error (the number of elements in the header doesn't match the number of elements in $row)
Do I have to remove the 'id_bookstore' => 2 in each row from $row to avoid the error? If so how?
Or is there another way to solve it?
Here the code:
$file = fopen(book.csv, 'wb');
$delimiter = ';';
fputcsv($file, array("id", "description", "product-title", "references"), $delimiter);
foreach ($result as $row) {
fputcsv($file, $row, ';');
}
fclose($file);
Thank you very much.
Upvotes: 1
Views: 72
Reputation: 99
to remove 'id_bookstore' from the row just use unset function like below:
$file = fopen(book.csv, 'wb');
$delimiter = ';';
fputcsv($file, array("id", "description", "product-title", "references"), $delimiter);
foreach ($result as $row) {
unset($row['id_bookstore']);
fputcsv($file, $row, ';');
}
fclose($file);
Upvotes: 2