从具有特定游戏日期的xml获取值

时间:2017-08-25 06:52:46

标签: php xml multidimensional-array

$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 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>

现在更新了我的问题我想只在游戏日期=&#34; 08/25/2017和#34;加上只能获得2场比赛。 结果将是这样的

  

时间:下午1:45   线型:2   LASTUPDATED:10

     

时间:下午1:45   线型:2   LASTUPDATED:10

提前致谢。

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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