gpt4 book ai didi

php - 节点不再存在于 SimpleXML 中

转载 作者:行者123 更新时间:2023-12-04 06:30:26 25 4
gpt4 key购买 nike

我有一个似乎无法解决的问题。当 $array 为空时,下面的代码将返回一个 php 错误“Node no longer exists”。如果 $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;
}

更新:

这是我现在所拥有的和我得到的:

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;
}

最佳答案

这意味着您要获取的属性不存在。您应该检查数组是否为空

if (isset($array) && count($array) > 0)
$prinid = $array[0];

关于php - 节点不再存在于 SimpleXML 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5481844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com