foreach ($dom->find('.post h1 a') as $checkIfResultTrue2) {
$totalSearchResult = $checkIfResultTrue2->href;
if(count($totalSearchResult) > 1){
echo "more than 1";
}
}
为什么这没有打印超过 1 个?我尝试了.length,但意识到那是JS。我试了一下大小和数数,没有运气。当我回声$totalSearchResult时,那里有超过 1 个链接。.嗯,有什么问题?
因为您将$totalSearchResult
的值覆盖为字符串。如果$totalSearchResult
是一个数组,你应该按如下方式写入它:
$totalSearchResult = array();
foreach ($dom->find('.post h1 a') as $checkIfResultTrue2) {
$totalSearchResult[] = $checkIfResultTrue2->href;
...
}
您正在覆盖一个变量。将该变量作为数组,并添加该数组中的每个元素。应该是,
$totalSearchResult=array();
foreach ($dom->find('.post h1 a') as $checkIfResultTrue2) {
array_push($totalSearchResult, $checkIfResultTrue2->href); <---
if(count($totalSearchResult) > 1){
echo "more than 1";
}
}