Skip to content
Advertisement

PHP: Write values of associative array in a column of CSV file based on their keys

I have this associative array with a key “key1” and another “key2”:

JavaScript

this is my code for write the csv file:

JavaScript

result is something like. All the information in line:

JavaScript

Is there a way to write these values in the column based on their key? The result should be:

JavaScript

Advertisement

Answer

You can do this with a loop saving all your table keys. Then you check all the keys on each line, if the key is missing, create it with an empty string !

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement