XML / php foreach循环不循环所有数据

时间:2010-12-30 04:15:45

标签: php xml loops foreach

看来我的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个小时了。请帮忙:(

1 个答案:

答案 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