2

This should be really easy, but I'm just not seeing how to do it. I just need to access the value of the element document-id.

print_r($http_result_simplexml);

gives...

SimpleXMLElement Object ( [document-id] => 1234 ) 

I need to get this document ID number, but I'm not getting how to do it. I tried $http_result_simplexml['document-id'], but it doesn't work. What I'm understading is that 'document-id' is the element, and '1234' is the value of the element. Another method I've come across is $http_result_simplexml->element_name, but obvisouly, the minus sign in 'document-id' won't work there.. I'm sure this is something absurdly simple..

(please correct me if that's not called the "element")

laketuna
  • 3,832
  • 14
  • 59
  • 104

2 Answers2

1

Access the element you are looking for:
$document_id = $http_result_simplexml->{'document-id'}

$document_id will also be a SimpleXMLObject! so you have to cast the value, using either:
$document_id = (string)$document_id; or $document_id = (int)$document_id; depending on whether you want a string or integer.

print_r($document_id); //should give the result you want now

brian_d
  • 11,190
  • 5
  • 47
  • 72
  • I don't quite understand. do you mean, I need to cast like this? (string)$http_result_simplexml['document-id']? This doesn't seem to work. – laketuna Jul 11 '11 at 22:26
  • @user796837 Nor should it. You have a SimpleXMLObject returned. The keyword is "object", not an "array". You need to access elements with the proper notation. Show your code for specific help please. – brian_d Jul 11 '11 at 22:27
  • thank you. I understand now. The key is to realize that it's an object, not an array. – laketuna Jul 11 '11 at 22:41
1

You may also try the __toString(): http://php.net/manual/en/language.oop5.magic.php

$element = $http_result_simplexml->{'document-id'}->__toString();

should work also.

hakre
  • 193,403
  • 52
  • 435
  • 836
Igor
  • 2,619
  • 6
  • 24
  • 36