简体   繁体   English

PHP 出现问题“消息:count():参数必须是数组或实现可计数的 object”

[英]Problem with PHP “Message: count(): Parameter must be an array or an object that implements Countable ”

Im completely new to PHP, please dont judge me by code it is not of my doing Im just fixing someone elses poor code.我对 PHP 完全陌生,请不要用代码来评判我,这不是我做的,我只是在修复别人糟糕的代码。 I got a task in job to fix issue with count() function.我的任务是解决count() function 的问题。

Theres variable called $var which has a result function count() with variable in it.有一个名为$var的变量,其结果为 function count() ,其中包含变量。

the problem is that I get error on this line问题是我在这条线上出错了

$has = count($var); // Message: count(): Parameter must be an array or an object that implements Countable

Heres the snippet of the code:下面是代码片段:

if($l)
foreach($list[$key] as $k=>$v){
    $var = @$c->content->config['has'][$v['type']];
    $has = count($var);
    echo '<li class="',($k==$l-1?'last ':''),($has?'folder ':'file ' ),(isset($list[$v['url']])?'open ':''),'type_',($v['type']),'"><span onClick="tree_tog(this)"></span><a href="',site_url('panel/'.($has?'index':'edit').'/'.$v['lang'].'/'.$v['nid'],null,false),'">',character_limiter($v['name'],40),'</a>';
    if($v['type']!='root'&&isset($list[$v['url']]))
        pr($list,$v['url'],$c);
    
    echo '</li>';
}

How can I fix this error?我该如何解决这个错误? Anyone?任何人? please!!请!!

See the documentation for the count() function.请参阅count() function 的文档

In your code, you have,在您的代码中,您有,

$has = count($var);

This means you are trying to count the elements of $var or more specifically, the object or literal that the $var variable refers to.这意味着您正在尝试计算$var的元素,或者更具体地说,object 或$var变量所指的文字。 Now, the count function can count elements of an array or any other object that is said to be countable .现在,计数 function 可以对数组的元素或任何其他 object 的元素进行计数 Not everything is countable.不是所有的东西都是可数的。 For instance, if you have an integer variable $price = 200;例如,如果您有一个 integer 变量$price = 200; then the variable does not contain any elements in it, rather it has a number.那么变量中不包含任何元素,而是有一个数字。 So, the count function cannot count (the elements of) $price variable.因此,计数 function 不能计数$price变量的(元素)。 Although, you could measure the length of the variable but that would be different than counting.虽然,您可以测量变量的长度,但这与计数不同。

Simply put, you can count elements of an array or an object that has its properties structured in a way so that they can be countable.简而言之,您可以对数组或 object 的元素进行计数,这些元素的属性以一种可数的方式结构化。 Otherwise, count() will fail and show that warning.否则, count()将失败并显示该警告。

Try printing out the variable and see if you can figure out what's in it.试着打印出变量,看看你能不能弄清楚里面有什么。 There are several ways to print/debug in PHP objects,有几种方法可以在 PHP 对象中打印/调试,

print_r($var);

Or,或者,

var_dump($var);

Happy coding.快乐编码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 消息:count():参数必须是一个数组或实现 Countable 的 object - Message: count(): Parameter must be an array or an object that implements Countable 消息:count():参数必须是在 centos 上实现可数 codeigniter 的数组或 object - Message: count(): Parameter must be an array or an object that implements Countable codeigniter on centos 消息:count():参数必须是一个数组或实现 codeigniter model 中的 Countable 的 object - Message: count(): Parameter must be an array or an object that implements Countable in codeigniter model PHP警告:count():参数必须是数组或实现Countable的对象? - PHP Warning: count(): Parameter must be an array or an object that implements Countable? count():参数必须是一个数组或一个 object 实现 php 中的可计数错误 - count(): Parameter must be an array or an object that implements Countable error in php PHP 7.2: count(): 参数必须是数组或实现Countable的object - PHP 7.2: count(): Parameter must be an array or an object that implements Countable PHP 7.2 警告count():参数必须是数组或实现Countable的object - PHP 7.2 Warning count(): Parameter must be an array or an object that implements Countable PHP count():参数必须是一个数组或一个实现了Countable的对象 - PHP count(): Parameter must be an array or an object that implements Countable PHP:count():参数必须是数组或者实现了Countable的对象 - PHP: count(): Parameter must be an array or an object that implements Countable count():参数必须是在其中实现Countable的数组或对象 - count(): Parameter must be an array or an object that implements Countable in
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM