我有供应商提供的 XML 文件。我想从该文件中读取数据。我的 read.php 文件包含:$xml = simplexml_load_file('export.xml');$result = $xml->xpath("//product");$array= array(); foreach ($result as $product) { $id = (string)$product->id; $code = (string)$product->code; $ean = (string)$product->combinations->combination->ean; $quantity = (string)$product->combinations->combination->quantity; print_r('ID:' .$id. ' CODE:' .$code. ' EAN: '.$ean.' - <b>'.$quantity.'</b> szt.<br />');}XML 看起来像<product> <id>684</id> <code>113</code> <combinations> <combination> <ean>111</ean> <quantity>0</quantity> </combination> <combination> <ean>222</ean> <quantity>5</quantity> </combination> <combination> <ean>333</ean> <quantity>2</quantity> </combination> </combinations></product>它工作得很好,但 throw 的唯一第一个组合ID:684 CODE:113 EAN: 111 - 0 szt.但我还想要这个 ID 的其他组合ID:684 CODE:113 EAN: 222 - 5 szt.ID:684 CODE:113 EAN: 333 - 2 szt.在哪里寻找解决方案?
1 回答
呼唤远方
TA贡献1856条经验 获得超11个赞
简单地添加一个 foreach $product->combinations->combination。
例如:
$xml = simplexml_load_file('export.xml');
$result = $xml->xpath("//product");
$array= array();
foreach ($result as $product) {
$id = (string)$product->id;
$code = (string)$product->code;
foreach( $product->combinations->combination as $combination ){
$ean = (string)$combination->ean;
$quantity = (string)$combination->quantity;
print 'ID:' .$id. ' CODE:' .$code. ' EAN: '.$ean.' - <b>'.$quantity.'</b> szt.<br />';
}
}
- 1 回答
- 0 关注
- 109 浏览
添加回答
举报
0/150
提交
取消