我有一些代码在 PHP 7 以下运行的服务器上运行良好,但在 PHP 7 上我收到一条警告,需要删除。我需要修复代码以消除警告,我不能只是隐藏警告。
我的问题是 count() 函数。这是我收到的警告及其所引用的一小段代码。该数组可能包含许多元素,一些元素带有值,另一些元素带有空值。数组也有可能为空。我假设当数组为空时,即触发警告。所以我正在寻找一种方法来判断数组是否有 1 个或多个元素,有或没有空白值。只要有一个键,那么 if 语句就应该为 true。
PHP Warning: count(): Parameter must be an array or an object that implements Countable
$tb_operator_meta_json = get_post_meta($tableid, 'tb_operator_meta', true);
$tb_operator_meta = json_decode($tb_operator_meta_json, true);
$tb_operator_meta = wp_unslash($tb_operator_meta);
if (count($tb_operator_meta) > 0 && $tb_operator_meta != null) {
我不知道为什么这被标记为重复。如果您阅读我的帖子,它显然与其他帖子不一样。
As of PHP 7.2.0 https://www.php.net/manual/en/function.count.php#refsect1-function.count-changelog
count() 现在将对无效可数产生警告
传递给 array_or_countable 参数的类型。
http://php.net/manual/en/function.count.php http://php.net/manual/en/function.count.php
检查数组is_array()
在计数之前。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)