我正在尝试将CSV字符串解析为多维数组。我使用以下代码来执行此操作...
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);
}
}
最终结果如下:
$ = 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
' => '',
),
);
当我尝试回显$ results ['Vendor SKU']或var_dump它时,即使我看到上面设置的值,结果也是NULL。我还注意到顶部有一个语法错误('$ = array')。
话虽如此,我最终只是想从CSV中获取值,因为其中一些值将用于向第三方供应商发送订单信息。
有人能指出我如何修正输出的正确方向,这样我才能正确回显这些值吗?
谢谢!
答案 0 :(得分:2)
使用内置的php函数来解析你的csv文件,而不是重新发明轮子。
str_getcsv
http://php.net/manual/en/function.str-getcsv.php
在手册页上有一个很好的例子,基本上可以满足您的需求:
$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);
答案 1 :(得分:1)
简单,使用预设的CSV读取方法:
$fp = fopen('data.csv', 'r');
$rows = array();
while (($row = fgetcsv($fp)) !== false)
$rows[] = $row;
fclose($fp);
$rows
将包含data.csv
的所有数据作为二维数组。
有关详细信息,请参阅fgetcsv()
。