Generate a CSV file from a multidimensional array in php I can tell by looking at this code that I've gone about this in the wrong way.  Provided the string is correctly formatted (each index ofCSVData[][], would the following generate a CSV file?

There must be a more elegant way of taking my queries and building a CSV.  Looking at this code kinda makes me sick in the stomach.

class ShippingCSVGenerator
{
    public function generateShippingCSV($invoiceArray)
    {    
        $invoiceArray = $this-&gt;filterInvoices($invoiceArray);
        if(empty($invoiceArray))
        {
            // no parcels to fetch
            return;
        }
        $dispatchDataRes = \DispatchDataQuery::create()
            -&gt;where('DispatchData.printed = ?', $invoiceArray)
            -&gt;find();

        $index = 0;
        $CSVData = [];
        foreach($dispatchDataRes as $orders)
        {
            $parcelsRes = \ItemsInDispatchQuery::create()
                -&gt;where('ItemsInDispatch.orderNumber = ?', $orders-&gt;orderNumber)
                -&gt;find();

            $parcelIndex = 0;
            foreach($parclesRes as $parcel)
            {
                $CSVData[$index][$parcelIndex] = 
                    $orders-&gt;orderNumber() . ',' .
                    $orders-&gt;date() . ',' .
                    $orders-&gt;toName() . ',' .
                    $orders-&gt;destinationBuilding() . ',' .
                    $orders-&gt;destinationStreet() . ',' .
                    $orders-&gt;suburb() . ',' .
                    $orders-&gt;unused_7() . ',' .
                    $orders-&gt;postcode() . ',' .
                    $orders-&gt;state() . ',' .
                    $orders-&gt;country() . ',' .
                    $orders-&gt;email() . ',' .
                    $orders-&gt;phone() . ',' .
                    $orders-&gt;itemName() . ',' .
                    $orders-&gt;itemPrice() . ',' .
                    $orders-&gt;instructions() . ',' .
                    $parcel-&gt;weight() . ',' .
                    $orders-&gt;shippingMethod() . ',' .
                    $parcelIndex . ',' .
                    $orders-&gt;SKU() . ',' .
                    $orders-&gt;quantity() . ',' .
                    $orders-&gt;unused_21() . ',' .
                    $orders-&gt;unused_22() . ',' .
                    $orders-&gt;unused_23() . ',' .
                    $orders-&gt;unused_24() . ',' .
                    $parcel-&gt;height() . ',' .
                    $parcel-&gt;width() . ',' .
                    $parcel-&gt;length() . ',' .
                    $orders-&gt;courier() . ',' .
                    $orders-&gt;unused_29() . ',' .
                    $orders-&gt;unused_30();
                $parcelIndex++;
            }
            $index++;
        }

        header("Content-Type: text/csv");
        header('Content-disposition: attachment;filename=shipping.csv');
        $fp = fopen("php://output", "w");
        fputcsv ($fp, $header, "\t");
        foreach($CSVData as $order)
        {
            foreach($csvOrder as $csvParcel)
            {
                fputcsv($fp, $csvParcel, "\n");
            }
        }
        fclose($fp);
    }

    private function filterInvoices($invoiceArray)
    {
        if(empty($invoiceArray))
        {
            // no parcels to fetch
            return;
        }
        $filteredInvoices = [];
        $dispatchDataRes = \DispatchDataQuery::create()
            -&gt;where('DispatchData.printed = ?', $invoiceArray)
            -&gt;find();
        if($dispatchDataRes-&gt;isEmpty())
        {
            // no parcels to fetch
            return;
        }
        else
        {
            foreach($dispatchDataRes as $invoicesToFetch)
            {
                $filteredInvoices = $invoicesToFetch-&gt;getInvoiceNumber();
            }
        }
        return $filteredInvoices;
    }
}
Encapsulate the csv line generation in another function.

function GenerateCSVlineFromArray($array){
    $line = "";
    foreach ($array as $value){
        $line .= $value .",";
    }
    //cut last comma
    $line = substr($line, 0, strlen($line) -1);
    return $line;
}


Assuming you have already the right order of values.