3 回答

TA貢獻1719條經驗 獲得超6個贊
在 PHP7.2
中,在嘗試計算不可數事物時添加了警告。要使其修復更改此行:
if(count($item[2]) > 0){
有了這個:
if(is_array($item[2]) && count($item[2]) > 0){
在 PHP7.3
中添加了一個新函數is_countable
,專門用于解決該E_WARNING
問題。如果您使用的是 PHP 7.3
,那么您可以更改此行:
if(count($item[2]) > 0){
有了這個:
if(is_countable($item[2]) && count($item[2]) > 0){

TA貢獻1829條經驗 獲得超7個贊
試試下面的代碼:
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];
}
}
}
}
檢查它

TA貢獻1851條經驗 獲得超4個贊
我相信在某些情況下,這會$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
}
- 3 回答
- 0 關注
- 109 瀏覽
添加回答
舉報