0

我刚刚将我的PHP 版本从 5.6 升级到了 7.4。我在我的页面中使用了 count()函数,例如:

$watch_server_count  = count($watch_server);
if ($watch_server_count > 0) {
    foreach ($watch_server->table as $key=> $watch_server_rows) {

    }
}

警告:count():参数必须是数组或实现 Countable 的对象...

4

2 回答 2

0

从 PHP 7.1 开始,您可以is_iterable在执行之前使用foreach.

(PHP 7 >= 7.1.0) is_iterable — 验证变量的内容是否为可迭代值

https://www.php.net/manual/en/function.is-iterable.php

所以代码将如下所示:

if ( is_iterable($watch_server->table) ) {
    foreach ($watch_server->table as $key=> $watch_server_rows) {
        //
    }
}
于 2020-05-26T09:26:40.480 回答
0

你可以试试这个方法。is_countable

https://www.php.net/manual/en/function.is-countable.php

if ( is_countable($watch_server->table) ) {
    foreach ($watch_server->table as $key=> $watch_server_rows) {
        ...
    }
}
于 2020-08-12T14:36:12.993 回答