Php 简单xml读取foreach

Php 简单xml读取foreach,php,xml,foreach,xml-parsing,Php,Xml,Foreach,Xml Parsing,不知你是否能在圣诞节帮忙 我正在尝试读取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</descri

不知你是否能在圣诞节帮忙

我正在尝试读取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>`
`
比赛博彩
只赢
−
画
−
3.5
5/2
2.
−
博尔顿流浪者队
−
2
1/1
1.
−
狼
−
3.6
13/5
3.
`
PHP

`xpath(//事件”);
foreach($游戏作为$事件)
{
回显“事件ID:.$Event['ID']。”
; 回显“事件日期:.$Event['Date']。”
; { foreach($event->children()作为$market) { 如果($market['period']=='FT') { foreach($market->children()作为$output) { 回显“结果ID:.$output['ID']。”
; foreach($outcome->children()作为$price) { 回显“价格ID:.$Price['ID']。”
; foreach($price->children()作为$value) { 回显“值:”.$Value.“
”; } } } } } } 回声“
”; } ?>`
这基本上是返回这个:

事件ID:298640100
活动日期:2011年12月31日星期六16:00:00 CET
结果ID:
结果ID:
结果ID:6798861400
价格标识:
价格ID:24532283602
数值:3.5
值:5/2
价格标识:
结果ID:6798861200
价格标识:
价格ID:24532283402
值:2.0
值:1/1
价格标识:
结果ID:6798861300
价格标识:
价格ID:2453228502
数值:3.6
数值:13/5
价格标识:

理想情况下,我只想返回以下内容:

事件ID:298640100
活动日期:2011年12月31日星期六16:00:00 CET
结果ID:6798861400
价格ID:24532283602
价值:5/2

任何关于我做错了什么以及如何做到这一点的想法

提前谢谢


Richard你这里有两个问题。首先,您使用的是children()函数,它返回所有子级,而不仅仅是您想要的特定类型。这就是为什么一开始你会得到3次结果ID。您应该使用
foreach($market->children()作为$output)
而不是
foreach($market->output作为$output)

第二,你似乎只想要第一个结果。在这种情况下,您不应该使用foreach。simplexml对象是一组数组,您可以通过其索引号访问数组中的单个对象。您可以去掉大量代码,直接获取第一个结果对象,如下所示:

$xml->event->market->outcome[0]

您可能需要阅读simpleXML官方文档

以下是我认为您需要的:

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 $name => $outcome ) 
                                {
                                    if ( $name != "outcome" )
                                    {
                                      continue;
                                    }
                                    echo "<b>Outcome ID: - $name</b> " . $outcome['id'] . "<br />";
                                    foreach ($outcome->children() as $name => $price) 
                                    {
                                      if ( $name != "price" )
                                      {
                                        continue;
                                      }
                                        echo "<b>Price ID:</b> " . $price ['id'] . "<br />";
                                        foreach ($price->children() as $name => $value)
                                            {
                                                if ( $name != "fractional" )
                                                {
                                                    continue;
                                                }
                                                echo "<b>Value: - $name</b> " . $value . "<br />";
                                                break;
                                            }
                                    }
                                                break;
                                }
                                                break;
                        }
                }
        } 
    echo "<br />";
}
foreach($game as$event)
{
回显“事件ID:.$Event['ID']。”
; 回显“事件日期:.$Event['Date']。”
; { foreach($event->children()作为$market) { 如果($market['period']=='FT') { foreach($market->children()作为$name=>$output) { 如果($name!=“结果”) { 继续; } echo“结果ID:-$name”。$output['ID']。”
; foreach($outcome->children()作为$name=>$price) { 如果($name!=“价格”) { 继续; } 回显“价格ID:.$Price['ID']。”
; foreach($price->children()作为$name=>$value) { 如果($name!=“分数”) { 继续; } echo“Value:-$name.”$Value.“
”; 打破 } } 打破 } 打破 } } } 回声“
”; }
非常感谢您的支持,我想我可能需要几天的时间来解决这个问题,非常感谢。有没有一种方法可以让我在描述中加入这一点?我还想把它存储起来,以显示赔率是平局、赢还是输。例如,Bolton Waverters嗯,我相信如果你将if($name!=“price”)更改为if($name!=“price”&&&$name!=“description”),你也可以得到描述。
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 $name => $outcome ) 
                                {
                                    if ( $name != "outcome" )
                                    {
                                      continue;
                                    }
                                    echo "<b>Outcome ID: - $name</b> " . $outcome['id'] . "<br />";
                                    foreach ($outcome->children() as $name => $price) 
                                    {
                                      if ( $name != "price" )
                                      {
                                        continue;
                                      }
                                        echo "<b>Price ID:</b> " . $price ['id'] . "<br />";
                                        foreach ($price->children() as $name => $value)
                                            {
                                                if ( $name != "fractional" )
                                                {
                                                    continue;
                                                }
                                                echo "<b>Value: - $name</b> " . $value . "<br />";
                                                break;
                                            }
                                    }
                                                break;
                                }
                                                break;
                        }
                }
        } 
    echo "<br />";
}