我想知道如何用 php 解析这个 xml 提要?
http://www.shinyloot.com/feeds/games_on_sale
我知道我可以用它开始:
$shinyloot = simplexml_load_file('http://www.shinyloot.com/feeds/games_on_sale');
从那里我不确定解析它的最佳方法是它是一种更复杂的方法,其中包含多个数组。
此外,这不是重复,它是一个特定案例,您链接的答案对于此提要不正确,请将其取消标记为重复。
我想知道如何用 php 解析这个 xml 提要?
http://www.shinyloot.com/feeds/games_on_sale
我知道我可以用它开始:
$shinyloot = simplexml_load_file('http://www.shinyloot.com/feeds/games_on_sale');
从那里我不确定解析它的最佳方法是它是一种更复杂的方法,其中包含多个数组。
此外,这不是重复,它是一个特定案例,您链接的答案对于此提要不正确,请将其取消标记为重复。
您可以使用来读取属性数据,对于在字母之间带有破折号和其他字符的元素,您可以用大括号和单引号将其括起来,就像我为元素$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,然后将其转换回简单的关联数组。
好的,这就是我想知道的任何人:
<?php
$url = 'http://www.shinyloot.com/feeds/games_on_sale';
$xml = simplexml_load_string(file_get_contents($url));
foreach ($xml->games->game as $game)
{
$os_options = array();
foreach ($game->{'operating-systems'}->os as $os)
{
$os_options[] = $os;
}
if (in_array("Linux", $os_options))
{
echo "Title: ", $game['title'], "\n";
echo "URL: ", $game['url'], "\n";
echo "Price: ", $game->price, "\n";
echo "<br />==================================================<br />";
}
}
不确定这是否是最好的方法,但这允许我按 os.filter 过滤。
感谢大奖赛。