您可以使用来读取属性数据,对于在字母之间带有破折号和其他字符的元素,您可以用大括号和单引号将其括起来,就像我为元素$variable['attribute_name']
所做的那样。operating-systems
<?php
$url = 'http://www.shinyloot.com/feeds/games_on_sale';
$xml = simplexml_load_string(file_get_contents($url));
foreach ($xml->games->game as $game)
{
$operating_system = array();
foreach ($game->{'operating-systems'}->os as $os)
$operating_system[] = $os;
if (!in_array("Linux", $operating_system))
continue;
echo "Title: ", $game['title'], "\n";
echo "URL: ", $game['url'], "\n";
echo "MRSP: ", $game->mrsp, "\n";
echo "Price: ", $game->price, "\n";
echo "Discount: ", $game->{'discount-pct'}, "%\n";
echo "Cover Image: ", $game->{'cover-image'}, "\n";
echo "Header Image: ", $game->{'header-image'}, "\n";
echo "Available for:\n";
foreach ($operating_system as $os)
{
echo $os, "\n";
}
echo "==================================================\n\n";
}
另一种方法是这样的:
$operating_system = json_decode(json_encode($game->{'operating-systems'}), true);
if (!in_array("Linux", $operating_system['os']))
continue;
基本上它将结果转换为 JSON,然后将其转换回简单的关联数组。