数组以获取对象';s值在php中失败
为什么这是错误的数组以获取对象';s值在php中失败,php,xml,Php,Xml,为什么这是错误的 [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [url] => http://www.thestar.com.my/~/media/Images/TSOL/Photos-Gallery/features/2014/07/02/dominiclau020714.ash
[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)
它不起作用。为什么?这不是获取属性值的正确方法。使用方法:
或
右快速格式
如果您尝试
$eachItem->enclosure->{'@attributes'}['url']
或$eachItem->enclosure->['@attributes']['url']
作为@attributes
是一个数组
而不是一个对象,该怎么办?获取属性的正确方法是使用->attributes()
方法+1这是您应该执行的操作,我忽略了SimpleXML。它变成了这个SimpleXMLElement对象([0]=>)SimpleXMLElement对象([0]=>)@user3522729添加一个类型转换字符串(字符串)
,检查修饰符,使用->附件本身的数组解引用。@Jack yeah可能会使用这个字符串,OP没有公布整个结构,这样更容易分析。你会看到OP发布了一堆关于这个问题的新问题。使用其他帐户。
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'};