2017-08-25 42 views
0
$xml_data = simplexml_load_file('demo.xml'); 

foreach ($xml_data->Game as $game) { 
$t = $game['Time']; 
$odds = $game->xpath('SportsBook[@Name="3Dimes"]/Odds'); 
echo 'Time: ' . $t . ' LineType: ' . $odds[0]->attributes()['LineType'] 
     . ' LastUpdated: ' . $odds[0]->attributes()['LastUpdated'] . PHP_EOL; 
} 

demo.xml從XML提取值與特定的遊戲時間

<?xml version="1.0" encoding="utf-8"?> 
<GameOdds SportsCode="MLB"> 
    <Game Code="121" Date="08/25/2017" Time="1:45 PM" HomeBoardNumber="11"  
      Home="home" RoadBoardNumber="951" Road="Road" Note=""> 
     <SportsBook ID="5" Name="5Dimes"> 
      <Odds LineType="2" LastUpdated="10" /> 
     </SportsBook> 
     <SportsBook ID="5" Name="3Dimes"> 
      <Odds LineType="3" LastUpdated="20" /> 
     </SportsBook> </Game> 
    <Game Code="122" Date="07/02/2017" Time="1:45 PM" HomeBoardNumber="11" Home="home" RoadBoardNumber="931" Road="Road" Note=""> 
     <SportsBook ID="2" Name="5Dimes"> 
      <Odds LineType="2" LastUpdated="10" /> 
     </SportsBook> 
     <SportsBook ID="6" Name="3Dimes"> 
      <Odds LineType="3" LastUpdated="30" /> 
     </SportsBook> 
    </Game> 
    <Game Code="121" Date="08/25/2017" Time="1:45 PM" HomeBoardNumber="11" Home="home" RoadBoardNumber="951" Road="Road" Note=""> 
     <SportsBook ID="5" Name="5Dimes"> 
      <Odds LineType="2" LastUpdated="10" /> 
     </SportsBook> 
     <SportsBook ID="5" Name="3Dimes"> 
      <Odds LineType="3" LastUpdated="30" /> 
     </SportsBook> </Game> 
    <Game Code="121" Date="08/31/2017" Time="1:45 PM" HomeBoardNumber="11" Home="home" RoadBoardNumber="951" Road="Road" Note=""> 
     <SportsBook ID="5" Name="5Dimes"> 
      <Odds LineType="2" LastUpdated="10" /> 
     </SportsBook> 
     <SportsBook ID="5" Name="3Dimes"> 
      <Odds LineType="3" LastUpdated="20" /> 
     </SportsBook> </Game> 
    <Game Code="121" Date="08/25/2017" Time="1:45 PM" HomeBoardNumber="11" Home="home" RoadBoardNumber="951" Road="Road" Note=""> 
     <SportsBook ID="5" Name="5Dimes"> 
      <Odds LineType="2" LastUpdated="10" /> 
     </SportsBook> 
     <SportsBook ID="5" Name="3Dimes"> 
      <Odds LineType="3" LastUpdated="20" /> 
     </SportsBook> </Game> 
</GameOdds> 

更新了我的問題,現在我想獲取只有GAME日期=「2017年8月25日」而且只能取2場比賽中只。 結果會是這樣

時間:1:45 PM 線型:2 LASTUPDATED:10

時間:1:45 PM 線型:2 LASTUPDATED:10

提前致謝。

回答

1

試試這個簡單的,希望這個會有幫助。

Try this code snippet here

$xml_data = simplexml_load_file('demo.xml'); 
$result=$xml_data->xpath('//Game[@Date="08/25/2017"]');//querying with XPath 
$result=array_slice($result, 0,2);//slicing data for two results 
foreach ($result as $game) 
{ 
    $odds = $game->xpath('SportsBook[@Name="3Dimes"]/Odds'); 
    $t = $game['Time']; 
    echo 'Time: ' . $t . ' LineType: ' . $odds[0]->attributes()['LineType'] 
     . ' LastUpdated: ' . $odds[0]->attributes()['LastUpdated'] . PHP_EOL; 
} 
+0

感謝您的幫助! – Asher

0

用下面的辦法:

$xml_data = simplexml_load_file('demo.xml'); 

foreach ($xml_data->xpath('//Game[@Date="08/25/2017" and position() <= 3]') as $game) { 
    $t = $game['Time']; 
    $odds = $game->xpath('SportsBook[@Name="5Dimes"]/Odds'); 
    echo 'Time: ' . $t . ' LineType: ' . $odds[0]->attributes()['LineType'] 
     . ' LastUpdated: ' . $odds[0]->attributes()['LastUpdated'] . PHP_EOL; 
} 

輸出:

Time: 1:45 PM LineType: 2 LastUpdated: 10 
Time: 1:45 PM LineType: 2 LastUpdated: 10