开发者

Need to access value in SimpleXML object [duplicate]

This question already has answers h开发者_开发百科ere: SimpleXML Reading node with a hyphenated name (2 answers) Closed 8 years ago.

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")


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜