访问对象键PHP中的特殊字符

时间:2014-04-25 05:08:13

标签: php arrays object

我有一个像这样的对象 -

    SimpleXMLElement Object
    (
         [links] => SimpleXMLElement Object
         (
              [@attributes] => Array
              (
                [total-matched] => 5
                [records-returned] => 5
                [page-number] => 1
              )
         )
    )

我尝试了以下语句,但它正在打印空对象 -

     print_r($data->links->{'@attributes'});

它只是打印空白对象,如下所示 -

   SimpleXMLElement Object{
   }

如何修复它并访问“完全匹配”元素?

2 个答案:

答案 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进行投射。