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

foreach($xml->Game as $d) {

foreach( $d->SportsBook as $sb) {

foreach( $sb->Odds as $odds) {
  //   echo $odds['LineType'].' '.$odds['LastUpdated']."<br/>";

}
} 
}

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>

Updated my question now i want to fetch only if GAME Date="08/25/2017" Plus only fetch 2 games only. how can i show specific Odds value into each Game. Something like 3Dimes values only. Game value and only 3dimes values from each Game . Result will be like this

Time:1:45 PM LineType:3 LastUpdated:20

Time:1:45 PM LineType:3 LastUpdated:20

Thanks in advance.

4 Answers 4

1

The solution using SimpleXMLElement::xpath function:

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

The output:

Time: 1:45 PM LineType: 3 LastUpdated: 20
Time: 1:45 PM LineType: 3 LastUpdated: 30
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help but now i want to fetch value if Date="08/25/2017" @romanperekhrest
@Asher, you could initiate a new question for the new issue
1

Rather than trying to navigate the layers of the XML you can use XPath to find the parts your interested in...

$xml = simplexml_load_file('t1.xml');

$xpath = $xml->xpath('//SportsBook[@Name="3Dimes"]/Odds');

foreach ($xpath as $odds)   {
    echo 'Time:'.(string)$odds->xpath('parent::*/parent::*/@Time')[0]
            .' LineType:'. $odds["LineType"]
            .' LastUpdated:'.$odds["LastUpdated"]."\n";
}

The hardest thing with this way is that once you've gone down to the 'Odds' elements, you have to then go back up (using parent::*) to fetch the time part.

Comments

1

Please see: https://iconoun.com/demo/temp_asher.php

<?php // demo/temp_asher.php
/**
 * Dealing with XML document
 *
 * https://stackoverflow.com/questions/45083520/showing-specific-value-from-xml-array-through-foreach
 */
error_reporting(E_ALL);
echo '<pre>';

// TEST DATA FROM THE POST AT STACK
$xml = <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<GameOdds SportsCode="MLB">
  <Game Code="121" Date="07/06/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>
</GameOdds>
EOD;

// MAKE AN OBJECT
$obj = simplexml_load_string($xml);

// ACTIVATE THIS TO VISUALIZE THE OBJECT
// var_dump($obj);

// EXTRACT DATA FROM THE OBJECT
foreach ($obj->Game as $g)
{
    $t = (string)$g->attributes()->Time;
    foreach ($g->SportsBook as $s)
    {
        // SKIP ANYTHING THAT IS NOT THE "Name" WE WANT
        $n = (string)$s->attributes()->Name;
        if ($n != '3Dimes') continue;

        $o = $s->Odds;
        $olt = (string)$o->attributes()->LineType;
        $olu = (string)$o->attributes()->LastUpdated;

        // SHOW THE INFORMATION FROM THE OBJECT
        echo PHP_EOL;
        echo "Time:$t ";
        echo "LineType:$olt ";
        echo "LastUpdated:$olu ";
    }
}

Comments

1

Xpath expressions allow you to fetch parts of an DOM using paths and conditions. In your case you want to output the Odds in SportsBook elements with an specific attribute. This is fairly straightforward in Xpath.

  • fetch any SportsBook element node
    //SportsBook
  • with a specific Name attribute value
    //SportsBook[@Name="3Dimes"]
  • fetch the Odds child nodes of them
    //SportsBook[@Name="3Dimes"]/Odds

This will return an DOMNodelist (if you're using DOM) or an array (if your're using SimpleXML). The Time attribute is not on the Odds node but an ancestor node. So we use that axis.

  • fetch the Game ancestor nodes
    //ancestor::Game
  • their Time attributes
    ancestor::Game/@Time
  • case the first (nearest) into a string
    string(ancestor::Game/@Time)

The last step (casting into string) only works with DOMXpath::evaluate(). So here is a complete DOM example (The other answers show the SimpleXML logic already).

$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);

$expression = '//SportsBook[@Name="3Dimes"]/Odds';
foreach ($xpath->evaluate($expression) as $odds) {
  printf(
    "Time:%s LineType:%d LastUpdated:%d\n",
    $xpath->evaluate('string(ancestor::Game/@Time)', $odds),
    $odds->getAttribute('LineType'),
    $odds->getAttribute('LastUpdated')
  );
}

Output:

Time:1:45 PM LineType:3 LastUpdated:20
Time:1:45 PM LineType:3 LastUpdated:30

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.