0
$xml_data = simplexml_load_file('demo.xml');
foreach ($xml_data->Game as $game) {
$t = $game['Time'];
$odds = $game->xpath('SportsBook[@Name="5Dimes"]/Odds');
if(isset($odds)){
$valueLT=$odds[0]->attributes()->LineType;
echo 'Time: ' . $t . ' LineType: '.$valueLT
. '<br/>' . PHP_EOL;
}
}
demo.xml顯示屬性值0,如果它的到來爲空(PHP)(XML)
<GameOdds SportsCode="MLB">
<Game Code="121" Date="07/06/2017" Time="1:45 PM">
<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="07/06/2017" Time="1:45 PM">
<SportsBook ID="5" Name="5Dimes" />
<SportsBook ID="5" Name="3Dimes">
<Odds LineType="3" LastUpdated="20" />
</SportsBook> </Game>
<Game Code="122" Date="07/02/2017" Time="1:46 PM">
<SportsBook ID="2" Name="5Dimes">
<Odds LineType="4" LastUpdated="10" />
</SportsBook>
<SportsBook ID="6" Name="3Dimes">
<Odds LineType="36" LastUpdated="30" />
</SportsBook>
</Game></GameOdds>
現在我得到致命錯誤是我沒有在第二場比賽賠率數據。如果我在任何體育博彩中沒有任何5Dimes賠率。我只想在那裏顯示價值。
Fatal error: Call to a member function attributes() on null
結果
Time: 1:45 PM LineType:2
Time: 1:45 PM LineType:0
Time: 1:46 PM LineType:4
感謝
因此,檢查'$賠率'值。我想'$ odds'總是__set__,所以'isset'是真的。改爲使用「空」。 –