[英]Error = Warning: count(): Parameter must be an array or an object that implements Countable in
[英]Wordpress error: Warning: count(): Parameter must be an array or an object that implements Countable
第一行是問題代碼。 我不知道如何將該計數更改為可以工作的值。
if(count($item[2]) > 0){
if($item[2][0] == 'plane' || $item[2][0] == 'url'){
if($item[2][0] == 'url'){
$arr = explode('file/d/',$id);
$arr1 = explode('/',$arr[1]);
$id = $arr1[0];
}
}
}
?>
在 PHP 7.2
,在嘗試計算不可數事物時添加了警告。 要修復它,請更改此行:
if(count($item[2]) > 0){
有了這個:
if(is_array($item[2]) && count($item[2]) > 0){
在 PHP 7.3
中添加了一個新函數is_countable
,專門用於解決E_WARNING
問題。 如果您使用的是 PHP 7.3
,則可以更改此行:
if(count($item[2]) > 0){
有了這個:
if(is_countable($item[2]) && count($item[2]) > 0){
我相信在某些情況下,此$item[2]
返回null
或任何其他不可數值。 從PHP 7開始,您將無法計算未實現可數的對象。 所以你需要先檢查它是否是一個數組:
if(is_countable($item[2])){ // you can also use is_array($item[2])
if(count($item[2]) > 0){
//rest of your code
}
}
另一種方法(雖然不是首選)是將您的對象傳遞給ArrayIterator
。 這將使它可迭代:
$item_2 = new ArrayIterator($item[2]);
if(count($item_2) > 0){
//rest of your code
}
試試下面的代碼:
if (is_array($item[2]) || $item[2] instanceof Countable || is_object($item[2])) {
if(count($item[2]) > 0){
if($item[2][0] == 'plane' || $item[2][0] == 'url'){
if($item[2][0] == 'url'){
$arr = explode('file/d/',$id);
$arr1 = explode('/',$arr[1]);
$id = $arr1[0];
}
}
}
}
檢查它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.