Skip to content
Advertisement

How can I delete empty elements from an array intended for a CSV export

I’m creating an array to export as a CSV file. I have about a dozen line items on my page that represents parts for an invoice IE qty, description, cost etc. Sometimes all of the line items are used sometimes only a few. Here is the array:

JavaScript

Here is the result in CSV

JavaScript

I need to remove the empty lines IE the ones that have 6 commas (,,,,,,). This example has 8 lines that need to be removed. Sometimes it’s just a couple or none. I’ve looked for 2 days now and tried numerous things like “if” statements to check if the first element is set, array_filter, array_diff etc but I haven’t found anything that works. Any help would be appreciated.

Advertisement

Answer

As already mentioned – use array_chunk to split your $exportStuff array into arrays (chunks) of size 7:

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