需要访问 SimpleXML 对象中的值
这应该很容易,但我只是不知道该怎么做。我只需要访问元素 document-id 的值。
print_r($http_result_simplexml);
给出...
SimpleXMLElement Object ( [document-id] => 1234 )
我需要获取此文档 ID 号,但我不知道如何操作。我尝试了 $http_result_simplexml['document-id'],但它不起作用。我的理解是“document-id”是元素,“1234”是元素的值。我遇到的另一种方法是 $http_result_simplexml->element_name ,但显然,“document-id”中的减号在那里不起作用..我确信这是非常简单的事情..
(请纠正我,如果这不叫“元素”)
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")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
访问您要查找的元素:
$document_id = $http_result_simplexml->{'document-id'}
$document_id
也将是一个SimpleXMLObject
!所以你必须使用以下任一方法来转换值:$document_id = (string)$document_id;
或$document_id = (int)$document_id;
取决于您想要字符串还是整数。
print_r($document_id); //现在应该给出你想要的结果
Access the element you are looking for:
$document_id = $http_result_simplexml->{'document-id'}
$document_id
will also be aSimpleXMLObject
! 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
您也可以尝试
__toString()
:http:// /php.net/manual/en/language.oop5.magic.php也应该可以工作。
You may also try the
__toString()
: http://php.net/manual/en/language.oop5.magic.phpshould work also.