dongyue1988 2018-04-16 10:51
浏览 18
已采纳

PHP计数替换[重复]

This question already has an answer here:

I have some code that works fine on servers running below PHP 7, but on PHP 7 I get a warning that I need to get rid of. I need to fix the code to get rid of the warning, I can not just hide the warnings.

My issue is with the count() function. Here is the warning I am getting and the little bit of code it is referring to. The array has the possibility of having many elements, some with values and others with blank values. It is also possible that the array will be empty. I assume that when the array is empty, that is when the warning is triggered. So I am looking for a way to tell if the array has 1 or more elements, with and without blank values. As long as there is one key then the if statement should be 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) {

I don't know why this was marked as a duplicate. If you read my post it is clearly not the same as the other post.

</div>
  • 写回答

2条回答 默认 最新

  • douyuqing_12345 2018-04-16 10:53
    关注

    PHP 7.2.0 - count() will now yield a warning on invalid countable types passed to the array_or_countable parameter. - http://php.net/manual/en/function.count.php

    check the array is_array() before counting.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效