Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用foreach()和php表单以及自定义函数_Php_Arrays_Foreach - Fatal编程技术网

使用foreach()和php表单以及自定义函数

使用foreach()和php表单以及自定义函数,php,arrays,foreach,Php,Arrays,Foreach,我有一个get函数,它返回输入的项目,效果很好。这在输入类中: public static function get($item){ if(isset($_POST[$item])){ return $_POST[$item]; } else if(isset($_GET[$item])){ return $_GET[$item]; } return ''; } 现在我试图在foreach循环中使用它,但遇到了一些问题-我

我有一个get函数,它返回输入的项目,效果很好。这在输入类中:

public static function get($item){
    if(isset($_POST[$item])){
        return $_POST[$item];
    } else if(isset($_GET[$item])){
        return $_GET[$item];
    }
    return '';
    }
现在我试图在foreach循环中使用它,但遇到了一些问题-我认为这是因为它没有返回数组

以下是表单片段:

<tr>
    <td><input type="text" class="form-control" name="inventory_item_id[]"></td>
    <td><input type="text" class="form-control" name="inventory_record_amount[]"></td>
    <td><input type="text" class="form-control" name="inventory_record_orders[]"></td>
    <td><input type="text" class="form-control" name="inventory_record_sales[]"></td>
</tr>
PhpStorm对我大喊大叫说:

为foreach提供的参数无效。预期类型:数组或对象,实际类型:字符串


有什么问题吗?

当您第一次进入页面时,可能会出现这种情况。这是因为在第一个请求中,您没有任何参数do use GET,也没有用POST请求加载页面,因此您还没有任何数组

为避免此错误,您无法验证$invItems是否为数组

$invItems = Input::get('inventory_item_id');
if (is_array($invItems)){
  foreach($invItems as $a => $b) {
     //some code here
  }
}

你能在$invItems=Input::get'inventory\u item\u id'之后变量转储$invItems吗;要查找invItems包含的内容,您的方法是否在每种情况下都返回一个数组?如果没有,那么您将看到错误仍然存在。可能会命中空字符串。否则,post值实际上是一个字符串。在我看来,没有其他可能的解释。只要确保它真正包装在表单标记中,而不是空字段
$invItems = Input::get('inventory_item_id');
if (is_array($invItems)){
  foreach($invItems as $a => $b) {
     //some code here
  }
}