PHP - Creating an array from CSV and then echo the values -
i attempting parse csv strings multi-dimensional array. using following code this...
public function exportpartsauthority($filearray) { foreach ($filearray $filename => $filecontent) { $lines = explode("\n", $filecontent); $formatting = explode(",", $lines[0]); unset($lines[0]); $results = array(); foreach ( $lines $line ) { $parsedline = str_getcsv( $line, ',' ); $result = array(); foreach ( $formatting $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 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 attempt echo $results['vendor sku'], or var_dump it.. result null though see value set above. notice there syntax error @ top ('$ = array').
that being said, i'm trying values csv because of these values used send order information third party vendor.
can point me in right direction of how fix output i'll able echo values correctly?
thanks!
use built in php function parse csv file rather re-invent wheel.
str_getcsv http://php.net/manual/en/function.str-getcsv.php
there's example on man page 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);
Comments
Post a Comment