douxiong3245 2019-07-16 08:42
浏览 54
已采纳

如何避免看似有效数组的php可数错误

I'm getting a php count() error, 'Warning: count(): Parameter must be an array or an object that implements Countable' on what is clearly an array. The code still works, but I'd like to know how to recode to avoid the warning messages.

First, I have a multidimensional array (print_f dump):

$icons Array
(
[0] => Array
    (
        [image] => 12811
        [label] => Chemical
        [categories] => Array
            (
                [0] => 209
            )

    )

[1] => Array
    (
        [image] => 12812
        [label] => Cut
        [categories] => Array
            (
                [0] => 236
            )

    )

[2] => Array
    (
        [image] => 12813
        [label] => Flame
        [categories] => Array
            (
                [0] => 256
                [1] => 252
            )

    )
)

And I'm matching up Wordpress terms to images:

<?php 
$terms = wp_get_post_terms( get_the_ID(), 'product_categories', array("fields" => "ids"));
if($icons) {

foreach($icons as $row) {
    for($i=0; $i<count($row['categories']); $i++) {
        for($j=0; $j<count($terms); $j++) {
            if($row['categories'][$i]==$terms[$j]) {
                       array_push($icon_img_ary,$row['image']);
                                $icon_img_ary_unq=wg_unique_array($icon_img_ary);
                       }
                }
          }
      }
}
} ?>

The error occurs in the first for() loop while counting the nested array. I've actually been using this same code for months now, with two instances on two separate documents. I'm only getting this error on one of the documents. I've been pulling my hair out trying to understand why the array not typing as an array.

I've seen some solutions discussed that use the array variable && count($array) in a conditional?? It's like an all new syntax that then beings to throw errors on subsequent ';' or {} characters. Very confusing, I'm trying to get an understanding. Any help would be much appreciated, Thanks!

  • 写回答

2条回答 默认 最新

  • duankekan9269 2019-07-16 09:24
    关注

    You can use is_countable() if you are using PHP 7.3 otherwise you can use is_array().

    For PHP 7.3 or above:

    <?php 
    $terms = wp_get_post_terms( get_the_ID(), 'product_categories', array("fields" => "ids"));
    if($icons) {
    
        foreach($icons as $row) {
            if ( is_countable( $row['categories'] ) ) {
                for($i=0; $i<count($row['categories']); $i++) {
                    for($j=0; $j<count($terms); $j++) {
                        if($row['categories'][$i]==$terms[$j]) {
                            array_push($icon_img_ary,$row['image']);
                            $icon_img_ary_unq=wg_unique_array($icon_img_ary);
                        }
                    }
                }
            }
        }
    }
    ?>
    

    For below PHP 7.3:

    <?php 
    $terms = wp_get_post_terms( get_the_ID(), 'product_categories', array("fields" => "ids"));
    if($icons) {
    
        foreach($icons as $row) {
            if ( is_array( $row['categories'] ) ) {
                for($i=0; $i<count($row['categories']); $i++) {
                    for($j=0; $j<count($terms); $j++) {
                        if($row['categories'][$i]==$terms[$j]) {
                            array_push($icon_img_ary,$row['image']);
                            $icon_img_ary_unq=wg_unique_array($icon_img_ary);
                        }
                    }
                }
            }
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭