0

I'm getting the following data and I need to be able to use PHP to put this data into a multi-dimensional array so that I can use the data as variables later on...

'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

"100000002","02/10/2013","pending","Main Website - Main Website Store - Default Store View","checkmo","flatrate_flatrate","2.0000","10.0000","12.0000","0.0000","","","K&N Air Filter Wrap","YA-6504PK K&N","","","1.0000","","1.0000","0.0000","0.0000","1","Brian","","","","","","","Sunrise","33323","Florida","US","Brian","","","","","","","Sunrise","33323","Florida","US","","",

"100000002","02/10/2013","pending","Main Website - Main Website Store - Default Store View","checkmo","flatrate_flatrate","2.0000","10.0000","12.0000","0.0000","","","K&N Air Filter Wrap","YA-6601-TDK K&N","","","1.0000","","1.0000","0.0000","0.0000","1","Brian","","","","","","","Sunrise","33323","Florida","US","Brian","","","","","","","Sunrise","33323","Florida","US","","",

"100000003","07/10/2013","pending","Main Website - Main Website Store - Default Store View","checkmo","flatrate_flatrate","1716.5000","5.0000","1721.5000","0.0000","","","Cardone High Pressure Diesel Injection Oil Pump","2P-225 Cardone","","","1716.5019","","1.0000","0.0000","0.0000","1","Brian","","","","","","","Sunrise","33323","Florida","US","Brian","","","","","","","Sunrise","33323","Florida","US","2P-225","A1",

'

I've been toying around with different PHP functions to make this happen with this data. I don't have it in a file so while most examples open a file for the data, I don't have it available as a file but rather these strings. I know I'll need to use a foreach loop to get this done, but I have had no success in doing it correctly thus far.

I've tried the following code but the values weren't showing properly for some odd reason...

$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;
}

Any help is much appreciated!

0 Answers0