【发布时间】:2012-01-27 15:37:27
【问题描述】:
想知道你是否可以为圣诞节提供帮助。
我正在尝试读取 XML,但遇到了一些问题,基本上 foreach 结构不允许我以我想要的结构返回数据,而且我不确定这样做的正确方法。示例如下:
`<event id="298640100" date="Sat Dec 31 16:00:00 CET 2011">
<market id="9064667" type="1" status="Open" period="FT">
<description>Match Betting</description>
<place_terms>Win only</place_terms>
−
<outcome id="6798861400">
<description>Draw</description>
−
<price id="24532283602">
<decimal>3.5</decimal>
<fractional>5/2</fractional>
</price>
<position>2</position>
</outcome>
−
<outcome id="6798861200">
<description>Bolton Wanderers</description>
−
<price id="24532283402">
<decimal>2.0</decimal>
<fractional>1/1</fractional>
</price>
<position>1</position>
</outcome>
−
<outcome id="6798861300">
<description>Wolves</description>
−
<price id="24532283502">
<decimal>3.6</decimal>
<fractional>13/5</fractional>
</price>
<position>3</position>
</outcome>
</market>
</event>`
PHP
`<?php
$source = file_get_contents("vc.xml");
$xml = simplexml_load_string($source);
$game = $xml->xpath("//event");
foreach ($game as $event)
{
echo "<b>Event ID:</b> " . $event['id'] . "<br />";
echo "<b>Event Date:</b> " . $event['date'] . "<br />";
{
foreach ($event->children() as $market)
{
if ($market['period'] == 'FT')
{
foreach ($market->children() as $outcome)
{
echo "<b>Outcome ID:</b> " . $outcome['id'] . "<br />";
foreach ($outcome->children() as $price)
{
echo "<b>Price ID:</b> " . $price ['id'] . "<br />";
foreach ($price->children() as $value)
{
echo "<b>Value:</b> " . $value . "<br />";
}
}
}
}
}
}
echo "<br />";
}
?>`
这基本上是返回这个:
事件 ID:298640100
活动日期:2011 年 12 月 31 日星期六 16:00:00 CET
结果 ID:
结果 ID:
结果编号:6798861400
价格编号:
价格编号:24532283602
值:3.5
价值:5/2
价格编号:
结果编号:6798861200
价格编号:
价格编号:24532283402
价值:2.0
价值:1/1
价格编号:
结果编号:6798861300
价格编号:
价格编号:24532283502
值:3.6
价值:13/5
价格编号:
理想情况下,我只想返回以下内容:
事件 ID:298640100
活动日期:2011 年 12 月 31 日星期六 16:00:00 CET
结果编号:6798861400
价格编号:24532283602
值:5/2
任何想法我做错了什么以及如何实现这一目标。
提前致谢
理查德
【问题讨论】:
标签: php xml foreach xml-parsing