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条)

报告相同问题?