PHP-简单的XML属性问题

| 我使用PHP和Simple XML。 我使用了一个无法正常工作的循环:
foreach($item->Image->attributes()->source as $key => $value)
{
    echo $value;
}
在foreach中,我尝试告诉我要获取属性中列出的图像的\“ source \”。 上面的
$item
是在
foreach($xml_content->Section->Item as $item {}
上方的代码周围循环创建的(如果您需要知道它的来源) 我的对象看起来像这样:
object(SimpleXMLElement)#36 (4) {
    [\"Text\"]=>
        string(15) \"Vinbergs socken\"
    [\"Description\"]=>
        string(73) \"Vinbergs socken ingick i Faurås härad och ligger i Falkenbergs kommun.
    \"
    [\"Url\"]=>
        string(44) \"http://sv.wikipedia.org/wiki/Vinbergs_socken\"
    [\"Image\"]=>
         object(SimpleXMLElement)#38 (1) {
             [\"@attributes\"]=>
                  array(3) {
                      [\"source\"]=>
                            string(113) \"http://upload.wikimedia.org/wikipedia/commons/thumb/2/25/Faur%C3%A5s_Vinberg.svg/50px-Faur%C3%A5s_Vinberg.svg.png\"
                      [\"width\"]=>
                          string(2) \"50\"
                      [\"height\"]=>
                            string(2) \"41\"
                      }
             }
     }
我的帖子开头的循环有什么问题?     
已邀请:
        您正在尝试迭代字符串,而不是数组
$item->Image->attributes()->source
要迭代Image元素的所有属性,请使用
foreach ($item->Image->attributes() as $attributeName => $attributeValue) {
如果只想输出source属性的值,请不要重复使用速记
echo $item->Image[\'source\']
请参阅此演示和PHP手册中的SimpleXml基本用法示例。     

要回复问题请先登录注册