【问题标题】:Invalid argument supplied for foreach() for :::: foreach($field_length_array as $fieldname => $maxlength) {为 :::: foreach($field_length_array as $fieldname => $maxlength) {
【发布时间】:2020-08-27 10:54:54
【问题描述】:

警告:为 foreach() 提供的参数无效,::: foreach 参数的错误对我来说似乎一切正常

function check_required_fields($required_array){
    $fields_errors = array();
    foreach($required_array as $fieldname) {
        if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0))
            { $field_errors[] = $fieldname; }
    }
    return $field_errors;
}

下面的参数给出了错误 foreach($field_length_array as $fieldname => $maxlength) {

 function check_max_field_lengths($field_length_array) {
        $field_errors = array();
            foreach($field_length_array as $fieldname => $maxlength) {
            if(strlen(trim(mysqli_prep($_POST[$fieldname]))) > $maxlength) {
            $filed_errors[] = $fieldname;}
        }
        return $field_errors;
    }

【问题讨论】:

  • 什么是$field_length_array?显然它不是一个数组。
  • 调用函数的时候,传递的是什么值?

标签: php function foreach arguments


【解决方案1】:

可能是因为您为此函数传递的无效数组“$field_length_arraycheck_max_field_lengths()

使用print_r() 检查 $field_length_array 是否具有相同的期望值

echo "<pre>; print_r($field_length_array); echo "</pre>";

 **or** dd($field_length_array).  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-31
    • 2018-11-24
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多