SimpleXML 中不再存在节点
我有一个似乎无法解决的问题。当 $array
为空时,下面的代码将返回 php 错误“节点不再存在”。如果 $array 不为空,则可以正常工作。当 $array
为空时,$prinid = $array[0];
行将显示错误。
$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);
foreach($doc as $a => $b) {
if ($a == 'principal-list') {
$array = $b->principal->attributes();
}
}
$prinid = $array[0];
if (isset($array[0])) {
$currentuser = 1;
} else {
$currentuser = 0;
}
更新:
这是我现在所拥有的,我得到:
警告:count() [function.count]:节点不再存在于 * * * * * * *
$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);
foreach($doc as $a => $b) {
if ($a == 'principal-list') {
$array = $b->principal->attributes();
}
}
$currentuser = 0;
if (isset($array) && count($array) > 0) {
$prinid = $array[0];
$currentuser = 1;
}
I have a problem that I cannot seem to fix. The code below will return a php error "Node no longer exists" when $array
is empty. If $array
is not empty it works fine. The error will show up for the line with $prinid = $array[0];
when $array
is empty.
$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);
foreach($doc as $a => $b) {
if ($a == 'principal-list') {
$array = $b->principal->attributes();
}
}
$prinid = $array[0];
if (isset($array[0])) {
$currentuser = 1;
} else {
$currentuser = 0;
}
Update:
Here is what I have now and I get:
Warning: count() [function.count]: Node no longer exists in * * * * * * *
$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);
foreach($doc as $a => $b) {
if ($a == 'principal-list') {
$array = $b->principal->attributes();
}
}
$currentuser = 0;
if (isset($array) && count($array) > 0) {
$prinid = $array[0];
$currentuser = 1;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这意味着您尝试获取的属性不存在。您应该检查数组不为空
It means that the attribute you are trying to get isn't there. You should check that array isn't empty
重要的事情 - 检查
$b->principal
- 您需要检查此 xml 对象是否为空。如果这是 true,那么在任何尝试解析$b->principal->attributes()
时,您都会收到此错误。Important thing - check
$b->principal
- you need check this xml object for empty. If it is true then on any try resolve$b->principal->attributes()
you can get this error.