在PHP中访问类对象

时间:2010-10-27 18:10:50

标签: php xml arrays

有没有人知道如何从以下对象访问HREF:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [title] => Preview
            [rel] => enclosure
            [type] => image/jpeg
            [href] => http://a1.phobos.apple.com/us/r1000/008/Purple/94/ee/38/mzl.fupornmt.320x480-75.jpg
        )

)

4 个答案:

答案 0 :(得分:3)

表示XML元素的SimpleXML对象支持数组访问元素属性

(string)$simpleXMLElement['href']

如果您希望将属性与字符串进行比较或将其传递给需要字符串的函数,则需要(string)强制转换。否则,PHP将该属性视为对象。 (如果您也使用attributes()方法,则适用)

答案 1 :(得分:2)

使用SimpleXMLElement::attributes方法:

$attrs = $obj->attributes();
echo $attrs['href'];

答案 2 :(得分:0)

答案 3 :(得分:0)

我不能说“投票”,但正如Anpher所说,你只需要访问该属性:

$attrs = $obj->attribues(); // Gets you the "[@attributes]" array (which is a copy of the internal private property "attributes")

do_things_with($attrs['href']); // Accesses the element you want.