0

I am attempting to parse CSV strings into a multi-dimensional array. I am using the following code to do this...

public function exportPartsAuthority($fileArray)
{       

    foreach ($fileArray as $filename => $fileContent) {
        $lines = explode("\n", $fileContent);
        $formatting = explode(",", $lines[0]);
        unset($lines[0]);
        $results = array();
        foreach ( $lines as $line ) {
           $parsedLine = str_getcsv( $line, ',' );
           $result = array();
           foreach ( $formatting as $index => $caption ) {
              if(isset($parsedLine[$index])) {
                 $result[$formatting[$index]] = trim($parsedLine[$index]);
              } else {
                 $result[$formatting[$index]] = '';
              }
           }
           $results[] = $result;
        }

        $var_str = var_export($results, true);
        $var = "<?php\n\n\$$values = $var_str;\n\n?>";
        file_put_contents('/home/apndev/public_html/output.txt', $var);

    }       
}

The end result is this:

$ = array (
  0 => 
  array (
    'Ordernumber' => '100000002',
    'Orderdate' => '02/10/2013',
    'OrderStatus' => 'pending',
    'PurchasedWebsite' => 'Main Website - Main Website Store - Default Store View',
    'PaymentMethod' => 'checkmo',
    'ShippingMethod' => 'flatrate_flatrate',
    'Subtotal' => '2.0000',
    'ShippingCost' => '10.0000',
    'GrandTotal' => '12.0000',
    'TotalTax' => '0.0000',
    'TotalPaid' => '',
    'TotalRefunded' => '',
    'ItemName' => 'K&N Air Filter Wrap',
    'ItemSKU' => 'YA-6504PK K&N',
    'ItemISBN' => '',
    'ItemStock' => '',
    'ItemPrice' => '1.0000',
    'CostPrice' => '',
    'ItemOrdered' => '1.0000',
    'ItemInvoiced' => '0.0000',
    'ItemSent' => '0.0000',
    'CustomerID' => '1',
    'BillingFirstName' => 'Brian',
    'BillingLastName' => '',
    'BillingCompany' => '',
    'BillingE-Mail' => '',
    'BillingPhone' => '',
    'BillingAddress1' => '',
    'BillingAddress2' => '',
    'BillingCity' => '',
    'BillingPostcode' => '',
    'BillingState' => 'Florida',
    'BillingCountry' => 'US',
    'ShippingFirstName' => 'Brian',
    'ShippingLastName' => '',
    'ShippingCompany' => '',
    'ShippingE-Mail' => '',
    'ShippingPhone' => '',
    'ShippingAddress1' => '',
    'ShippingAddress2' => '',
    'ShippingCity' => '',
    'ShippingPostcode' => '',
    'ShippingState' => 'Florida',
    'ShippingCountry' => 'US',
    'Vendor SKU' => '',
    'Line Code
' => '',
  ),
  1 => 
  array (
    'Ordernumber' => '100000002',
    'Orderdate' => '02/10/2013',
    'OrderStatus' => 'pending',
    'PurchasedWebsite' => 'Main Website - Main Website Store - Default Store View',
    'PaymentMethod' => 'checkmo',
    'ShippingMethod' => 'flatrate_flatrate',
    'Subtotal' => '2.0000',
    'ShippingCost' => '10.0000',
    'GrandTotal' => '12.0000',
    'TotalTax' => '0.0000',
    'TotalPaid' => '',
    'TotalRefunded' => '',
    'ItemName' => 'K&N Air Filter Wrap',
    'ItemSKU' => 'YA-6601-TDK K&N',
    'ItemISBN' => '',
    'ItemStock' => '',
    'ItemPrice' => '1.0000',
    'CostPrice' => '',
    'ItemOrdered' => '1.0000',
    'ItemInvoiced' => '0.0000',
    'ItemSent' => '0.0000',
    'CustomerID' => '1',
    'BillingFirstName' => 'Brian',
    'BillingLastName' => '',
    'BillingCompany' => '',
    'BillingE-Mail' => '',
    'BillingPhone' => '',
    'BillingAddress1' => '',
    'BillingAddress2' => '',
    'BillingCity' => '',
    'BillingPostcode' => '',
    'BillingState' => 'Florida',
    'BillingCountry' => 'US',
    'ShippingFirstName' => 'Brian',
    'ShippingLastName' => '',
    'ShippingCompany' => '',
    'ShippingE-Mail' => '',
    'ShippingPhone' => '',
    'ShippingAddress1' => '',
    'ShippingAddress2' => '',
    'ShippingCity' => '',
    'ShippingPostcode' => '',
    'ShippingState' => 'Florida',
    'ShippingCountry' => 'US',
    'Vendor SKU' => '',
    'Line Code
' => '',
  ),
  2 => 
  array (
    'Ordernumber' => '100000003',
    'Orderdate' => '07/10/2013',
    'OrderStatus' => 'pending',
    'PurchasedWebsite' => 'Main Website - Main Website Store - Default Store View',
    'PaymentMethod' => 'checkmo',
    'ShippingMethod' => 'flatrate_flatrate',
    'Subtotal' => '1716.5000',
    'ShippingCost' => '5.0000',
    'GrandTotal' => '1721.5000',
    'TotalTax' => '0.0000',
    'TotalPaid' => '',
    'TotalRefunded' => '',
    'ItemName' => 'Cardone High Pressure Diesel Injection Oil Pump',
    'ItemSKU' => '2P-225 Cardone',
    'ItemISBN' => '',
    'ItemStock' => '',
    'ItemPrice' => '1716.5019',
    'CostPrice' => '',
    'ItemOrdered' => '1.0000',
    'ItemInvoiced' => '0.0000',
    'ItemSent' => '0.0000',
    'CustomerID' => '1',
    'BillingFirstName' => 'Brian',
    'BillingLastName' => '',
    'BillingCompany' => '',
    'BillingE-Mail' => '',
    'BillingPhone' => '',
    'BillingAddress1' => '',
    'BillingAddress2' => '',
    'BillingCity' => '',
    'BillingPostcode' => '',
    'BillingState' => 'Florida',
    'BillingCountry' => 'US',
    'ShippingFirstName' => 'Brian',
    'ShippingLastName' => '',
    'ShippingCompany' => '',
    'ShippingE-Mail' => '',
    'ShippingPhone' => '',
    'ShippingAddress1' => '',
    'ShippingAddress2' => '',
    'ShippingCity' => '',
    'ShippingPostcode' => '',
    'ShippingState' => 'Florida',
    'ShippingCountry' => 'US',
    'Vendor SKU' => '2P-225',
    'Line Code
' => 'A1',
  ),
  3 => 
  array (
    'Ordernumber' => '',
    'Orderdate' => '',
    'OrderStatus' => '',
    'PurchasedWebsite' => '',
    'PaymentMethod' => '',
    'ShippingMethod' => '',
    'Subtotal' => '',
    'ShippingCost' => '',
    'GrandTotal' => '',
    'TotalTax' => '',
    'TotalPaid' => '',
    'TotalRefunded' => '',
    'ItemName' => '',
    'ItemSKU' => '',
    'ItemISBN' => '',
    'ItemStock' => '',
    'ItemPrice' => '',
    'CostPrice' => '',
    'ItemOrdered' => '',
    'ItemInvoiced' => '',
    'ItemSent' => '',
    'CustomerID' => '',
    'BillingFirstName' => '',
    'BillingLastName' => '',
    'BillingCompany' => '',
    'BillingE-Mail' => '',
    'BillingPhone' => '',
    'BillingAddress1' => '',
    'BillingAddress2' => '',
    'BillingCity' => '',
    'BillingPostcode' => '',
    'BillingState' => '',
    'BillingCountry' => '',
    'ShippingFirstName' => '',
    'ShippingLastName' => '',
    'ShippingCompany' => '',
    'ShippingE-Mail' => '',
    'ShippingPhone' => '',
    'ShippingAddress1' => '',
    'ShippingAddress2' => '',
    'ShippingCity' => '',
    'ShippingPostcode' => '',
    'ShippingState' => '',
    'ShippingCountry' => '',
    'Vendor SKU' => '',
    'Line Code
' => '',
  ),
);

When I attempt to echo $results['Vendor SKU'], or var_dump it.. the result is NULL even though I see a value set above. I also notice there is a syntax error at the top ('$ = array').

That being said, I'm ultimately just trying to get the values from the CSV because some of these values will be used to send order information to a third party vendor.

Can anyone point me in the right direction of how to fix my output so that I'll be able to echo the values correctly?

Thanks!

2 Answers 2

2

use the built in php function to parse your csv file rather than re-invent the wheel.

str_getcsv

http://php.net/manual/en/function.str-getcsv.php

There's a good example on the man page that essentially does what you want:

$header = NULL;
    $data = array();
    if (($handle = fopen($filename, 'r')) !== FALSE)
    {
        while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
        {
            if(!$header)
                $header = $row;
            else
                $data[] = array_combine($header, $row);
        }
        fclose($handle);
    }
    print_r($data);
Sign up to request clarification or add additional context in comments.

Comments

1

Simple, use the preset CSV reading method:

$fp = fopen('data.csv', 'r');

$rows = array();
while (($row = fgetcsv($fp)) !== false)
    $rows[] = $row;

fclose($fp);

$rows will then contain all data of data.csv as a two-dimensional array.

See fgetcsv() for more info.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.