Skip to content
Advertisement

How to convert Array to Array list for CSV export using FputCSV?

I need to convert an array with multiple results to a CSV file but am struggling to get a valid output . After some debugging I realized I am not generating a valid array list for fputcsv to iterate through .

First of all, here is an illustrative example of a var_dump of my array :

JavaScript

And this is the code I have tried after some researching on how to convert an array to CSV :

JavaScript

I also tried to use the standard example from fputcsv :

JavaScript

Obviously I am doing something very wrong and failing to produce the proper format for inserting multiple array lines in the csv file .

If I replace my $data array with this :

JavaScript

Then fputcsv works as expected .

So what I need is to convert my array into this format so fputcsv can iterate through it and save each line .

How could I achieve this ?

Thanks

Advertisement

Answer

Just cast the objects to arrays with (array):

JavaScript

If the object properties and $HeaderFields are not the same then use $HeaderFields instead.

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