php中获取对象'值的数组失败


array to get object's value failed in php

为什么这是错误的?

[enclosure] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [url] => http://www.thestar.com.my/~/media/Images/TSOL/Photos-Gallery/features/2014/07/02/dominiclau020714.ashx?crop=1&w=460&h=345&
                    [length] => 
                    [type] => image/jpeg
                )
        )

我想要获取获取图像文件的url

我写了print_r($eachItem->enclosure['@attributes']->url),它不工作。为什么?

这不是获取属性值的正确方法。使用 ->attributes() 方法:

echo (string) $eachItem->enclosure->attributes()['url'];
// as of PHP 5.4 (dereferencing)

// PHP 5.3 below
$eachItem_attribute = $eachItem->enclosure->attributes();
echo (string) $eachItem_attribute['url'];

右&快速格式化

$eachItem->enclosure->attributes()->{'url'};