我正在尝试将PHP文件格式设置为由我的网站生成的XML文件,然后删除所有冗余信息并输出CSV文件。我有一切正常工作,除非有多个项目,只有第一个项目正在提交。这是我正在使用的XML和PHP,我无法弄清楚为什么它没有使用foreach命令循环。
XML
<line-items type="array">
<line-item>
<fulfillment-service>manual</fulfillment-service>
<fulfillment-status nil="true"/>
<gift-card type="boolean">false</gift-card>
<grams type="integer">5000</grams>
<id type="integer">56789</id>
<price type="decimal">199.99</price>
<product-id type="integer">123456</product-id>
<quantity type="integer">1</quantity>
<requires-shipping type="boolean">true</requires-shipping>
<sku>SKU2006-001</sku>
<taxable type="boolean">true</taxable>
<title>Sledgehammer</title>
<variant-id type="integer">654321</variant-id>
<variant-title nil="true"/>
<vendor nil="true"/>
<name>Sledgehammer</name>
<variant-inventory-management nil="true"/>
<properties type="array">
</properties>
<product-exists type="boolean">true</product-exists>
<fulfillable-quantity type="integer">1</fulfillable-quantity>
<tax-lines type="array"/>
</line-item>
<line-item>
<fulfillment-service>manual</fulfillment-service>
<fulfillment-status nil="true"/>
<gift-card type="boolean">false</gift-card>
<grams type="integer">500</grams>
<id type="integer">98765</id>
<price type="decimal">29.95</price>
<product-id type="integer">12345</product-id>
<quantity type="integer">1</quantity>
<requires-shipping type="boolean">true</requires-shipping>
<sku>SKU2006-020</sku>
<taxable type="boolean">true</taxable>
<title>Wire Cutter</title>
<variant-id type="integer">54321</variant-id>
<variant-title nil="true"/>
<vendor nil="true"/>
<name>Wire Cutter</name>
<variant-inventory-management nil="true"/>
<properties type="array">
</properties>
<product-exists type="boolean">true</product-exists>
<fulfillable-quantity type="integer">1</fulfillable-quantity>
<tax-lines type="array"/>
</line-item>
用PHP格式化它;
$line_items = $order->{'line-items'};
$line_item = $order->{'line-items'}->{'line-item'};
$count_line_item = count($line_item);
$item_sku = $order->{'line-items'}->{'line-item'}->{'sku'};
$item_qty = $order->{'line-items'}->{'line-item'}->{'quantity'};
$ship_to_email = $order->{'email'};
$item_price = $order->{'line-items'}->{'line-item'}->{'price'};
$item_array = array($line_item);
foreach($item_array as $line_item) {
$csv =
$order_number.','.
/*$order_contact_id.','.*/
$first_name.','.
$last_name.','.
$order_date.','.
$shipping_address1.','.
$shipping_address2.','.
$shipping_city.','.
$shipping_province.','.
$shipping_country.','.
$shipping_zip.','.
$shipping_phone.','.
$item_sku.','.
$item_qty.','.
$ship_to_email.','.
$item_price;
};
我希望输出类似 1234,Steve,Shipper,2015-01-09 08:40:30,123 Shipping Street ,, Shippington,KY,US,K2P0S0,555-555- SHIP,SKU2006-001,1,jon @ doe.ca,199.99 但如果有多个项目则连续。
思想?
答案 0 :(得分:0)
根据您需要/想要的转换次数,它可以像
一样简单<?php
$fp = fopen('php://stdout', 'wb');
$items = new SimpleXMLElement( data() );
foreach( $items->{'line-item'} as $item ) {
fputcsv($fp, iterator_to_array($item));
}
function data() {
return <<< eox
<line-items type="array">
<line-item>
<fulfillment-service>manual</fulfillment-service>
<fulfillment-status nil="true"/>
<gift-card type="boolean">false</gift-card>
<grams type="integer">5000</grams>
<id type="integer">56789</id>
<price type="decimal">199.99</price>
<product-id type="integer">123456</product-id>
<quantity type="integer">1</quantity>
<requires-shipping type="boolean">true</requires-shipping>
<sku>SKU2006-001</sku>
<taxable type="boolean">true</taxable>
<title>Sledgehammer</title>
<variant-id type="integer">654321</variant-id>
<variant-title nil="true"/>
<vendor nil="true"/>
<name>Sledgehammer</name>
<variant-inventory-management nil="true"/>
<properties type="array">
</properties>
<product-exists type="boolean">true</product-exists>
<fulfillable-quantity type="integer">1</fulfillable-quantity>
<tax-lines type="array"/>
</line-item>
<line-item>
<fulfillment-service>manual</fulfillment-service>
<fulfillment-status nil="true"/>
<gift-card type="boolean">false</gift-card>
<grams type="integer">500</grams>
<id type="integer">98765</id>
<price type="decimal">29.95</price>
<product-id type="integer">12345</product-id>
<quantity type="integer">1</quantity>
<requires-shipping type="boolean">true</requires-shipping>
<sku>SKU2006-020</sku>
<taxable type="boolean">true</taxable>
<title>Wire Cutter</title>
<variant-id type="integer">54321</variant-id>
<variant-title nil="true"/>
<vendor nil="true"/>
<name>Wire Cutter</name>
<variant-inventory-management nil="true"/>
<properties type="array">
</properties>
<product-exists type="boolean">true</product-exists>
<fulfillable-quantity type="integer">1</fulfillable-quantity>
<tax-lines type="array"/>
</line-item>
</line-items>
eox;
}
打印
manual,,false,5000,56789,199.99,123456,1,true,SKU2006-001,true,Sledgehammer,654321,,,Sledgehammer,,"
",true,1,
manual,,false,500,98765,29.95,12345,1,true,SKU2006-020,true,"Wire Cutter",54321,,,"Wire Cutter",,"
",true,1,
编辑:示例仅选择少数元素
<?php
$fp = fopen('php://stdout', 'wb');
$items = new SimpleXMLElement( data() );
$data = array(
'onum' => 1234,
'fname' => 'Volker',
'sku' => 0,
'qty' => 0,
'shipemail' => 'foo@b.ar'
);
foreach( $items->{'line-item'} as $item ) {
$data['sku'] = (string)($item->sku);
$data['qty'] = (string)($item->quantity);
fputcsv($fp, $data);
}
function data() {
return <<< eox
<line-items type="array">
<line-item>
<fulfillment-service>manual</fulfillment-service>
<fulfillment-status nil="true"/>
<gift-card type="boolean">false</gift-card>
<grams type="integer">5000</grams>
<id type="integer">56789</id>
<price type="decimal">199.99</price>
<product-id type="integer">123456</product-id>
<quantity type="integer">5</quantity>
<requires-shipping type="boolean">true</requires-shipping>
<sku>SKU2006-001</sku>
<taxable type="boolean">true</taxable>
<title>Sledgehammer</title>
<variant-id type="integer">654321</variant-id>
<variant-title nil="true"/>
<vendor nil="true"/>
<name>Sledgehammer</name>
<variant-inventory-management nil="true"/>
<properties type="array">
</properties>
<product-exists type="boolean">true</product-exists>
<fulfillable-quantity type="integer">1</fulfillable-quantity>
<tax-lines type="array"/>
</line-item>
<line-item>
<fulfillment-service>manual</fulfillment-service>
<fulfillment-status nil="true"/>
<gift-card type="boolean">false</gift-card>
<grams type="integer">500</grams>
<id type="integer">98765</id>
<price type="decimal">29.95</price>
<product-id type="integer">12345</product-id>
<quantity type="integer">17</quantity>
<requires-shipping type="boolean">true</requires-shipping>
<sku>SKU2006-020</sku>
<taxable type="boolean">true</taxable>
<title>Wire Cutter</title>
<variant-id type="integer">54321</variant-id>
<variant-title nil="true"/>
<vendor nil="true"/>
<name>Wire Cutter</name>
<variant-inventory-management nil="true"/>
<properties type="array">
</properties>
<product-exists type="boolean">true</product-exists>
<fulfillable-quantity type="integer">1</fulfillable-quantity>
<tax-lines type="array"/>
</line-item>
</line-items>
eox;
}
打印
1234,Volker,SKU2006-001,5,foo@b.ar
1234,Volker,SKU2006-020,17,foo@b.ar