$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>
现在更新了我的问题我想只在游戏日期=“08/25/2017”时获取,而且只能获取2个游戏。 如何在每场比赛中显示特定的赔率值。像3Dimes这样的东西只是值。每个游戏的游戏价值和仅3dimes值。 结果将是这样的
时间:下午1:45 线型:3 LASTUPDATED:20
时间:下午1:45 线型:3 LASTUPDATED:20
提前致谢。
答案 0 :(得分:1)
您可以使用XPath找到您感兴趣的部分,而不是尝试浏览XML层...
$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";
}
这种方式最难的是,一旦你进入'赔率'元素,你必须返回(使用parent::*
)来获取时间部分。
答案 1 :(得分:1)
使用SimpleXMLElement::xpath函数的解决方案:
$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;
}
输出:
Time: 1:45 PM LineType: 3 LastUpdated: 20
Time: 1:45 PM LineType: 3 LastUpdated: 30
答案 2 :(得分:1)
请参阅:http://www.cplusplus.com/reference/ios/ios_base/precision/
<?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 ";
}
}
答案 3 :(得分:1)
Xpath表达式允许您使用路径和条件获取DOM的一部分。在您的情况下,您希望在Odds
元素中输出具有特定属性的SportsBook
。这在Xpath中非常简单。
SportsBook
元素节点//SportsBook
Name
属性值//SportsBook[@Name="3Dimes"]
Odds
个子节点
//SportsBook[@Name="3Dimes"]/Odds
这将返回DOMNodelist(如果您正在使用DOM)或数组(如果您使用的是SimpleXML)。 Time
属性不在Odds
节点上,而是在祖先节点上。所以我们使用那个轴。
Game
祖先节点//ancestor::Game
Time
属性ancestor::Game/@Time
string(ancestor::Game/@Time)
最后一步(强制转换为字符串)仅适用于DOMXpath::evaluate()
。所以这是一个完整的DOM示例(其他答案已经显示了SimpleXML逻辑)。
$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')
);
}
输出:
Time:1:45 PM LineType:3 LastUpdated:20
Time:1:45 PM LineType:3 LastUpdated:30