看来我的foreach正在循环正确的次数。但是,它只使用第一个循环中的内容填充变量。
我尝试了两种方法。但首先这是XML feed的url http://wowfeeds.wipeitau.com/GuildActivity.php?location=EU&rn=shadowsong&gn=antheas&output=XML&callback=?所以你可以看到结构。
php codee issss
function GetAchievements(){
$achurl = "http://wowfeeds.wipeitau.com/GuildActivity.php?location=EU&rn=shadowsong&gn=antheas&limit=100&output=XML&callback=?";
$achxml = new SimpleXMLElement($achurl);
// Achievements
foreach ($achxml->ACTIVITYLIST->ACTIVITYITEM as $ach) {
$name = $ach['NAME'];
echo $name;
//$Achievments = "<p><img src='$achimg' /> <span class='red'>$achname</span> $achtext <span class='red'>$achobj</span></p>";
//echo $Achievments;
}
}
这似乎只是一个空白。
但是,如果我将代码@ $ name = $ ach ['NAME']更改为= $ name =
function GetAchievements(){
$achurl = "http://wowfeeds.wipeitau.com/GuildActivity.php?location=EU&rn=shadowsong&gn=antheas&limit=100&output=XML&callback=?";
$achxml = new SimpleXMLElement($achurl);
// Achievements
foreach ($achxml->ACTIVITYLIST->ACTIVITYITEM as $ach) {
$name = $achxml->ACTIVITYLIST->ACTIVITYITEM->NAME;
echo $name;
//$Achievments = "<p><img src='$achimg' /> <span class='red'>$achname</span> $achtext <span class='red'>$achobj</span></p>";
//echo $Achievments;
}
}
然后它只是重复第一个条目与条目相同的次数。
EG。 名称。 名称。 名称。 这让我疯了2个小时了。请帮忙:(
答案 0 :(得分:1)
simplexml对象不是数组,您可能需要像这样考虑
$url = 'http://wowfeeds.wipeitau.com/GuildActivity.php?'.
'location=EU&rn=shadowsong&gn=antheas&output=XML&callback=?';
$achxml = simplexml_load_file($url);
foreach ($achxml->ACTIVITYLIST->ACTIVITYITEM as $ach)
{
$name = (string) $ach->NAME;
echo $name, "\n";
}
输出:
Ichex Azraelka Brechnor Rougwar Bromious Ziini Ryoden Ashlynne Snappidagg Flökræ Flökræ Sevenfold Ashlynne Bonewing Goldstroke Flökræ Worgin Bromious Renevatio Ziini Flökræ Flökræ Strollomiona Thorban Ichex