如何避免似乎是有效数组的 php 可数错误 [英] How to avoid php countable errors on what appear to be valid arrays

查看:21
本文介绍了如何避免似乎是有效数组的 php 可数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个 php count() 错误,警告:count():参数必须是一个数组或一个实现 Countable 的对象",显然是一个数组.代码仍然有效,但我想知道如何重新编码以避免出现警告消息.

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.

首先,我有一个多维数组(print_f dump):

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
            )

    )
)

我正在将 Wordpress 术语与图像匹配:

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);
                       }
                }
          }
      }
}
} ?>

错误发生在第一个 for() 循环中,同时对嵌套数组进行计数.我实际上已经使用相同的代码几个月了,在两个单独的文档上有两个实例.我只在其中一份文件上收到此错误.我一直在努力理解为什么数组不作为数组输入.

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.

我看过一些讨论的解决方案,它们使用数组变量 &&count($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!

推荐答案

如果你使用的是 PHP 7.3 你可以使用 is_countable() 否则你可以使用 is_array().

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

对于 PHP 7.3 或更高版本:

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);
                    }
                }
            }
        }
    }
}
?>

对于低于 PHP 7.3:

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);
                    }
                }
            }
        }
    }
}
?>

这篇关于如何避免似乎是有效数组的 php 可数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆