gpt4 book ai didi

javascript - 尝试获取非对象的属性 -> 如何检查对象是否存在

转载 作者:行者123 更新时间:2023-12-03 09:27:47 24 4
gpt4 key购买 nike

在使用 Dom Document 时如何检查对象是否存在,因为我正在获取

error "Trying to get property of non-object"

$getplacename            = '//table//tr';
$getplacename2 = $xpath->query($getplacename);
//loop through each individual card
foreach($getplacename2 as $getplacename22) {

echo $getplacename22 ->childNodes->item(0)->childNodes->item(2)->textContent;
echo $getplacename22 ->childNodes->item(4)->textContent;
echo "<br /><br /><br />";
}

最佳答案

嗯,我认为执行此类操作的最简单方法是将代码更改为类似的内容(假设错误发生在以下行之一中):

foreach($getplacename2 as $getplacename22) {
if ($getplacename22->childNodes->item(0) != null && $getplacename22->childNodes->item(0)->childNodes->item(2) != null) {
echo $getplacename22->childNodes->item(0)->childNodes->item(2)->textContent;
}
if($getplacename22 ->childNodes->item(4) != null) {
echo $getplacename22->childNodes->item(4)->textContent;
}
echo "<br /><br /><br />";
}

关于javascript - 尝试获取非对象的属性 -> 如何检查对象是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31609780/

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