我有这样的表结构,我需要摆动她的foreach循环来提取所有848个元素。问题是循环只显示一个项目而不是坐在那里的实际数量。通过常规的forem和idid替换工作的方式,虽然在我看来写同样的东西更长,更丑。
数组结构:
SimpleXMLElement Object
(
[@attributes] => Array
(
[language] => pol
[currency] => PLN
)
[product] => Array
(
[0] => SimpleXMLElement Object
[@attributes] => Array
(
[id] => 8
)
[1] => SimpleXMLElement Object
[@attributes] => Array
(
[id] => 4
)
[etc...]
)
我试试这种方式
$chairXML = simplexml_load_file('toparser.xml');
foreach ($chairXML->children() as $children) {
echo $children->product['id'];
}
但这不起作用,循环返回一条记录。
答案 0 :(得分:0)
$xml = '
<xml>
<product>
<cherry>1</cherry>
<apple>3</apple>
<orange>4</orange>
</product>
</xml>';
$simpleXmlObj = simplexml_load_string($xml);
foreach ($simpleXmlObj->product->children() as $children) {
print_r($children);
}
结果
SimpleXMLElement Object
(
[0] => 1
)
SimpleXMLElement Object
(
[0] => 3
)
SimpleXMLElement Object
(
[0] => 4
)
我认为你的问题在$ simpleXmlObj-&gt; children() 试试$ simpleXmlObj-&gt; product-&gt; children()