我想显示文件中的所有记录,例如所有代码,但它只显示第一条记录。
demo.xml
<?xml version="1.0" encoding="UTF-8"?>
<ProductData version="2.0">
<Product>
<code>1000001</code>
<name>The Name</name>
<brand>The Brand</brand>
<category>The Category</category>
<specification>
<characteristic>
<color>The Color</color>
<size>The Size</size>
</characteristic>
</specification>
<quantity>
<store1>0</store1>
<store2>4</store2>
<store3>5</store3>
<store4>2</store4>
<store5>3</store5>
</quantity>
</Product>
<Product>
<code>1000002</code>
<name>The Name</name>
<brand>The Brand</brand>
<category>The Category</category>
<specification>
<characteristic>
<color>The Color</color>
<size>The Size</size>
</characteristic>
</specification>
<quantity>
<store1>0</store1>
<store2>4</store2>
<store3>5</store3>
<store4>2</store4>
<store5>3</store5>
</quantity>
</Product>
</ProductData>
的index.php
<?php
if (file_exists('demo.xml')) {
$xml = simplexml_load_file('demo.xml');
echo $xml->Product->code;
} else {
exit('Error!');
}
?>
结果: 1000001
我正在寻找:1000001,1000002
我希望有人可以帮助我,因为这种方法对于我需要的东西非常实用,能够以与获得第一个值相同的方式获取所有代码,名称和所有信息会很棒。< / p>
答案 0 :(得分:2)
循环将成为你的朋友:
$codes = [];
$xml = simplexml_load_file('demo.xml');
foreach($xml->Product as $i => $product) {
$codes[] = $product->code;
}
echo implode(', ',$codes);
显然是一个简短的例子,但您也可以访问其他项目并使用它们。
答案 1 :(得分:0)