PHP - 从SimpleXML对象中访问带有带连字符名称的数组

时间:2014-07-14 15:46:30

标签: php simplexml

我在一个带有连字符名称的simpleXML对象中有一个数组。我使用了$ object-> {'array-name'}的方法,但它只返回数组的第一个元素,而不是我需要的整个数组。是否有不同的方法或使用此方法获取整个数组的不同方法?这是整个对象的print_r。

Array ( 
  [headers] => HTTP/1.1 200 OK Server: nginx Date: Mon, 14 Jul 2014 14:35:57 GMT Content-Type: application/xml; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Status: 200 OK Strict-Transport-Security: max-age=31536000 X-Records: 5 X-Pages: 1 X-Page: 1 ETag: "30a9b19d475d403ce69b9a5a61e80203" Cache-Control: max-age=0, private, must-revalidate X-Frame-Options: SAMEORIGIN X-UA-Compatible: IE=Edge,chrome=1 X-Runtime: 0.053259 X-Throttle-Count: 7 X-Throttle-Max: 500 X-Throttle-Horizon: 2014-07-14T14:36:00Z X-Request-Id: b234644f6dbd4a0fe1d558514e434780 X-Queue-Time: 0.0006668567657470703 Timing-Allow-Origin: * Vary: Accept-Encoding 
  [body] => SimpleXMLElement Object ( 
    [@attributes] => Array ( 
      [type] => array 
    ) 
    [time-entry] => Array ( 
      [0] => SimpleXMLElement Object ( 
        [date] => 2014-07-08 
        [description] => SimpleXMLElement Object ( ) 
        [hours] => 0.0833333 
        [id] => 87023284 
        [person-id] => 9820624 
        [project-id] => 12234988 
        [todo-item-id] => 186616792 
        [person-name] => Name 
      ) 
      [1] => SimpleXMLElement Object ( 
        [date] => 2014-07-08 
        [description] => SimpleXMLElement Object ( ) 
        [hours] => 0.0833333 
        [id] => 87023280 
        [person-id] => 9820624 
        [project-id] => 12234988 
        [todo-item-id] => 186616793 
        [person-name] => Name
      ) 
      [2] => SimpleXMLElement Object ( 
        [date] => 2014-07-08 
        [description] => SimpleXMLElement Object ( ) 
        [hours] => 0.166667 
        [id] => 87023271 
        [person-id] => 9820624 
        [project-id] => 12234988 
        [todo-item-id] => 186616794 
        [person-name] => Name 
      ) 
      [3] => SimpleXMLElement Object ( 
        [date] => 2014-07-08 
        [description] => SimpleXMLElement Object ( ) 
        [hours] => 0.166667 
        [id] => 87023264 
        [person-id] => 9820624 
        [project-id] => 12234988 
        [todo-item-id] => 186616795 
        [person-name] => Name 
      ) 
      [4] => SimpleXMLElement Object ( 
        [date] => 2014-07-08 
        [description] => SimpleXMLElement Object ( ) 
        [hours] => 0.166667 
        [id] => 87023262 
        [person-id] => 9820624 
        [project-id] => 12234988 
        [todo-item-id] => 186616797 
        [person-name] => Name 
      ) 
    ) 
  ) 
  [status] => 200 OK 
  [location] => 
  [records] => 5 
  [pages] => 1 
  [page] => 1 
)

我正在尝试获取数组时间条目,我需要整个数组。这样做的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

您可以使用括号语法来访问不遵循变量的允许命名规则的成员:

$element->{'time-entry'};