[英]Variable is an array but not countable?
I am upgrading our Codeigniter (v. 3.1.11) site from php 5.6 to php 7.2 (currently running on localhost on my Mac).我正在将我们的 Codeigniter(v. 3.1.11)站点从 php 5.6 升级到 php 7.2(当前在我的本地主机上运行)。 Slowly I am finding all the instances of count() usage and correcting them.
慢慢地,我找到了所有使用 count() 的实例并更正它们。 It is my understanding that an array is countable, but I seem to not be able to count arrays returned by Codeigniter's result_array() function after a database call....
据我了解,数组是可数的,但我似乎无法计算 Codeigniter 的 result_array() function 在数据库调用后返回的 arrays ......
the following section of my controller下面一段我的controller
$reviews = $this->reviews_model->review_details($productname);
echo "Variable is type: ".gettype($reviews);
if (count($reviews >=1)) {
$myreview=$reviews[0];
} else {
$myreview=0;
}
return $myreview;
calls this function in my model (note that I am echoing out the variable type just to be sure!)在我的 model 中调用这个 function (请注意,为了确定,我正在呼应变量类型!)
function review_details($pagename) {
$r = false;
$sql = "select Reviews.*, ReviewItemLink.Item as Product, ReviewItemLink.* from Reviews LEFT JOIN ReviewItemLink ON Reviews.ReviewItemID=ReviewItemLink.ReviewItemID where pagename=? AND ReviewActive = 1 ORDER BY Date DESC";
$query = $this->db->query($sql, $pagename);
if ($query->num_rows() > 0):
$r = $query->result_array();
endif;
return $r;
}
And even though the variable is an array即使变量是一个数组
Variable is type: array
I still get the by now, oh-so-familiar warning message:到现在为止,我仍然收到非常熟悉的警告信息:
Severity: Warning
Message: count(): Parameter must be an array or an object that implements Countable
Filename: controllers/Software.php
Line Number: 1005
Backtrace:
File: /Users/juner/Sites/html/application/controllers/Software.php
Line: 1005
Function: _error_handler
File: /Users/juner/Sites/html/application/controllers/Software.php
Line: 75
Function: _get_my_review
File: /Users/juner/Sites/html/index.php
Line: 324
Function: require_once
Are there types of arrays that are NOT countable?是否存在不可数的 arrays 类型? Any suggestions/ideas would be most helpful!
任何建议/想法都会很有帮助!
This part is incorrect (typo?):这部分不正确(错字?):
if (count($reviews >=1)) {
$myreview=$reviews[0];
} else {
$myreview=0;
}
You are passing a boolean to the count function.您将 boolean 传递给计数 function。
count($reviews >=1)
turns to count(true)
; count($reviews >=1)
变成count(true)
; which is why you got your warning.这就是你收到警告的原因。
if (count($reviews >=1)) {
should be if (count($reviews) >=1 ) {
if (count($reviews >=1)) {
应该是if (count($reviews) >=1 ) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.