我正在使用 YouTube API。我从 API 得到以下结果:
SimpleXMLElement Object
(
[@attributes] => Array
(
[rel] => alternate
[href] => http://www.youtube.com/watch?v=blabla
)
)
我对这个对象感到困惑。我想访问 @attributes
。我该怎么做?
最佳答案
print_r 输出的 @attributes
部分只是可以通过 $obj['attrname']
访问的元素的属性。
<?php
$obj = new SimpleXMLElement('<foo rel="alternate" href="http://www.youtube.com/watch?v=blabla" />');
print_r($obj); // to verify that the sample data fits your actual data
echo $obj['rel'], ' | ', $obj['href'];
SimpleXMLElement Object
(
[@attributes] => Array
(
[rel] => alternate
[href] => http://www.youtube.com/watch?v=blabla
)
)
alternate | http://www.youtube.com/watch?v=blabla
另见 Example #5 Using attributes在 SimpleXML 文档中。
关于php - 访问 xml 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35055603/