0

I have an array inside of a simpleXML object that has a hyphenated name. I have used the method of $object->{'array-name'}, however it is only return the first element of the array, not the entire array that I need. Is there a different method or a different way of using this method to get the entire array? Here's the print_r of the entire object.

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 
)

I am trying to get the array time-entry, and I need the entire array. What is the best method of doing this?

wat2020
  • 3
  • 1
  • Try $object['body']->{'time-entry'}; you may have skipped the body element. – ArtisticPhoenix Jul 14 '14 at 15:49
  • What do you mean by "only returns the first element of the array"? How are you testing this? Note that SimpleXML doesn't actually contain or return PHP arrays, it just provides array-like behaviour in situations that require it (e.g. `foreach`). – IMSoP Jul 22 '14 at 20:35

1 Answers1

0

You can use the bracket syntax to access members that are not following the allowed naming rules for a variable:

$element->{'time-entry'};
epicdev
  • 406
  • 3
  • 6
  • I have been using that syntax, however it is only giving me the first value in the array, instead of the whole array which I need. Is there a way to adapt it to return the whole array, or do I have to find a work-around to get all the values in the array? – wat2020 Jul 14 '14 at 15:51
  • Normally it should return the whole array. You can try with $element->children() maybe. – epicdev Jul 14 '14 at 15:57
  • 1
    Got it, thanks! Had to use the children method and cast it to an array first. Used this method: $element2 = (array)$element->children; $element2['time-entry']. That gave me the full array I needed. – wat2020 Jul 14 '14 at 16:11