【问题标题】:Message: sizeof(): Parameter must be an array or an object that implements Countable消息:sizeof():参数必须是数组或实现了 Countable 的对象
【发布时间】:2020-12-06 14:55:33
【问题描述】:

我在使用以下 PHP 版本的应用程序中遇到以下错误: 如果我可以在 PHP 版本上更改某些内容,您能否帮我说一下,它会起作用。我试图找到有这个错误的东西,但在他们的特定代码上。不得不提的是,比起 PHP 开发者,我更像是一名具备 Linux 技能的应用程序管理员。

PHP 7.4.9 (cli) (built: Aug  4 2020 08:28:13) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.9, Copyright (c), by Zend Technologies

遇到 PHP 错误 严重性:警告 消息:sizeof():参数必须是数组或实现了 Countable 的对象 文件名:库/MY_HTML.php 行号:89

代码:

 /**
     * 
     * @param string $name - select element "name" atribute
     * @param (array or stdClass) $option_array - option array with value and text
     * @param int $selectedIndex - which index is selected bby default
     * @param string $spec - selectbox specifications (html code, example: "id=\"select\" style=\"background-color: #FFF;\"")
     * @param string $selectedValue - option with this "value" is selected
     * @return string - select element to be printed
     */


        function createHolidaySelectBox($name, $option_array,
      $selectedIndex = 0, $spec = "", $selectedValue = NULL) {
            $select_box = "<select name=\"$name\" " . $spec . ">";
            $i = 0;
            foreach ($option_array as $v) {
                if ($v->isActive()) {
                    if (sizeof($v) < 2) {
                        $val = array_keys($option_array, $v);
                        $opt = array($val[0], $v);
                    } else {
                        $opt = $v;
                    }
    
                    $select_box.="<option value=\"" . $opt[0] . "\"" .               (($selectedValue == NULL) ? ($i == $selectedIndex ? " selected" : "") : 
 (($opt[0] == $selectedValue) ? " selected" : "")) . ">" . $opt[1] .                                 "</option>";
                    $i++;
                }
            }
            $select_box.="</select>";
            return $select_box;
        }
    
    }

【问题讨论】:

  • $v 是一个值,而不是一个数组。
  • 如果我降级 php 版本会将 $v 视为不显示此警告的值?
  • 您尝试过什么调试问题?你想达到什么目标?

标签: php


【解决方案1】:

这件作品

if ($v->isActive()) {
    if (sizeof($v) < 2) {
        $val = array_keys($option_array, $v);
        $opt = array($val[0], $v);
    } else {
        $opt = $v;
    }

应该这样改

if ($v->isActive()) {
    if (is_array($v) && sizeof($v) < 2) {
        $val = array_keys($option_array, $v);
        $opt = array($val[0], $v);
    } else {
        $opt = $v;
    }

这是因为查看您的代码时,您期望的是一个值(如字符串、bool int 等)或数组

【讨论】:

    猜你喜欢
    • 2018-09-05
    • 2019-06-02
    • 2020-05-28
    • 2019-01-27
    • 2021-05-30
    • 2018-11-18
    • 2019-10-04
    • 2019-07-23
    相关资源
    最近更新 更多