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:

$exportArray=array(
            array('Qty','Description','QBitem','UnitCost','Invoice#','Customer','Invoice Comments'), //this is the header row
            array($exportStuff[0],$exportStuff[1],$exportStuff[2],$exportStuff[3],$exportStuff[4],$exportStuff[5],$exportStuff[6]),
            array($exportStuff[7],$exportStuff[8],$exportStuff[9],$exportStuff[10],$exportStuff[11],$exportStuff[12],$exportStuff[13]),
            array($exportStuff[14],$exportStuff[15],$exportStuff[16],$exportStuff[17],$exportStuff[18],$exportStuff[19],$exportStuff[20]),
            array($exportStuff[21],$exportStuff[22],$exportStuff[23],$exportStuff[24],$exportStuff[25],$exportStuff[26],$exportStuff[27]),
            array($exportStuff[28],$exportStuff[29],$exportStuff[30],$exportStuff[31],$exportStuff[32],$exportStuff[33],$exportStuff[34]),
            array($exportStuff[35],$exportStuff[36],$exportStuff[37],$exportStuff[38],$exportStuff[39],$exportStuff[40],$exportStuff[41]),
            array($exportStuff[42],$exportStuff[43],$exportStuff[44],$exportStuff[45],$exportStuff[46],$exportStuff[47],$exportStuff[48]),
            array($exportStuff[49],$exportStuff[50],$exportStuff[51],$exportStuff[52],$exportStuff[53],$exportStuff[54],$exportStuff[55]),
            array($exportStuff[56],$exportStuff[57],$exportStuff[58],$exportStuff[59],$exportStuff[60],$exportStuff[61],$exportStuff[62]),
            array($exportStuff[63],$exportStuff[64],$exportStuff[65],$exportStuff[66],$exportStuff[67],$exportStuff[68],$exportStuff[69]),
            array($exportStuff[70],$exportStuff[71],$exportStuff[72],$exportStuff[73],$exportStuff[74],$exportStuff[75],$exportStuff[76]),
            array($exportStuff[77],$exportStuff[78],$exportStuff[79],$exportStuff[80],$exportStuff[81],$exportStuff[82],$exportStuff[83]),
            array($exportStuff[84],$exportStuff[85],$exportStuff[86],$exportStuff[87],$exportStuff[88],$exportStuff[89],$exportStuff[90]),
            array($exportStuff[91],$exportStuff[92],$exportStuff[93],$exportStuff[94],$exportStuff[95],$exportStuff[96],$exportStuff[97]),
            array($exportStuff[98],$exportStuff[99],$exportStuff[100],$exportStuff[101],$exportStuff[102],$exportStuff[103],$exportStuff[104]),
            array($exportStuff[105],$exportStuff[106],$exportStuff[107],$exportStuff[108],$exportStuff[109],$exportStuff[110],$exportStuff[111]),
            array($exportStuff[112],$exportStuff[113],$exportStuff[114],$exportStuff[115],$exportStuff[116],$exportStuff[117],$exportStuff[118]),
            array($exportStuff[119],$exportStuff[120],$exportStuff[121],$exportStuff[122],$exportStuff[123],$exportStuff[124],$exportStuff[125]),  
        );

Here is the result in CSV

Qty,Description,QBitem,UnitCost,Invoice#,Customer,"Invoice Comments",
1,"Blah blah blah","Quickbooks item",,BH-IT-646,Alpha,
2,"2020-05-18 Dirt, Joe Remote","Quickbooks item",110.00,BH-IT-646,Alpha,
1,"2020-05-19 Dirt, Joe Remote","Quickbooks item",110.00,BH-IT-646,Alpha,
2,"2020-05-20 Dirt, Joe On Site","Quickbooks item",110.00,BH-IT-646,Alpha,
1,"2020-05-21 Dirt, Joe In Shop","Quickbooks item",110.00,BH-IT-646,Alpha,
1,"2020-05-22 Dirt, Joe Remote","Quickbooks item",110.00,BH-IT-646,Alpha,
1,"Network Switch NG1001","Quickbooks item",312.5,BH-IT-646,Alpha,
4,"Data Jacks UNJ600OR","Quickbooks item",8.25,BH-IT-646,Alpha,
200,"Cat6 Cable 123456","Quickbooks item",2.31,BH-IT-646,Alpha,
1,"Trip Charge","Quickbooks item",25.00,BH-IT-646,Alpha,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,

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:

$exportArray = array(
    array('Qty','Description','QBitem','UnitCost','Invoice#','Customer','Invoice Comments'), //this is the header row
); 

$chunks = array_chunk($exportStuff, 7);
// Next you need to check if chunk has empty values only or not. 
// If not - add this chunk to `$exportArray`
foreach ($chunks as $chunk) {
    foreach ($chunk as $value) {
        if ($value) {
            $exportArray[] = $chunk;
            break;
        }
    }
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement