2011-12-25 74 views
0

不知道你是否可以幫助聖誕節。簡單的XML閱讀foreach

我想讀取一個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
活動日期:星期六12月31日16時00分00秒CET 2011
成果ID:
結果編號:
結果編號:6798861400
價格ID:
價格ID:24532283602
值:3.5
值:5/2
價格ID:
結果ID:6798861200
價格ID:
價格ID:24532283402
值:2.0
值:1/1
價格ID:
結果ID:6798861300
價格ID:
價格ID:24532283502
值:3.6
值:13/5
價格ID:

理想我只想返回如下:

事件ID:298640100
活動日期:星期六12月31日16:00:00 CET 2011
結果ID:6798861400
價格ID:24532283602
值:5/2

任何想法我做錯了什麼,我怎麼能做到這一點。

在此先感謝

理查德

回答

-1

以下是我認爲你需要:

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 />"; 
} 
+0

非常感謝你的這個,我認爲這需要花費我幾天的時間才能完成,非常感謝。我可以在描述中添加一些方法嗎?我也想保存這個以顯示賠率是平局還是輸贏。例如。 博爾頓流浪者 user989952 2011-12-25 16:38:30

+0

嗯,我相信如果您將if($ name!=「price」)更改爲if($ name!=「price」&& $ name!=「description」),您可以獲得描述也是如此。 – Yaniro 2011-12-25 20:14:46

2

您這裏有2個問題。首先,你正在使用children()函數,它返回所有的孩子,而不僅僅是你想要的特定類型。這就是爲什麼你在開始時得到成果ID:3次。您應該使用foreach ($market->outcome as $outcome)而不是foreach ($market->children() as $outcome)

其次,看起來你只想要第一個結果。在這種情況下,你不應該使用foreach。 simplexml對象是一組數組,您可以通過其索引號訪問數組中的單個對象。你可以擺脫大量代碼的,只是直接搶了先結果對象是這樣的:

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

你可能想閱讀官方SimpleXML的文檔http://www.php.net/manual/en/simplexml.examples-basic.php