我正在尝试将 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 中获取值,因为其中一些值将用于将订单信息发送给第三方供应商。
谁能指出我如何修复我的输出的正确方向,以便我能够正确地回显这些值?
谢谢!