我有一个像这样的对象 -
SimpleXMLElement Object
(
[links] => SimpleXMLElement Object
(
[@attributes] => Array
(
[total-matched] => 5
[records-returned] => 5
[page-number] => 1
)
)
)
我尝试了以下语句,但它正在打印空对象 -
print_r($data->links->{'@attributes'});
它只是打印空白对象,如下所示 -
SimpleXMLElement Object{
}
如何修复它并访问“完全匹配”元素?
答案 0 :(得分:2)
试试这个例子
echo (string)$data->links->attributes()['total-matched'];
echo (string)$data->links->attributes()['records-returned'];
echo (string)$data->links->attributes()['page-number'];
答案 1 :(得分:1)
如果你单独尝试print
这些元素,你可以选择这个......
echo (string)$data->links->attributes()->total-matched; //prints 5
echo (string)$data->links->attributes()->records-returned; //prints 5
echo (string)$data->links->attributes()->page-number; //prints 1
不要忘记如图所示对string
进行投射。