I have this associative array with a key "key1" and another "key2":
{
id : 1,
name : "Rob",
key1 : "bla"
},
{
id : 2,
name : "Mat",
key1 : "blabla"
},
{
id : 3,
name : "Tom",
key2 : "blablabla"
}
this is my code for write the csv file:
// $csv is the array
$fp = fopen('file.csv', 'wb');
foreach ($csv as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
result is something like. All the information in line:
1,Rob,bla
2,Mat,blabla
3,Tom,blablabla
Is there a way to write these values in the column based on their key? The result should be:
id | name | key1 | key2
1 | Rob | bla |
2 | Mat | blabla |
3 | Tom | | blablabla