Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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
如何在PHP中获取动态创建输入字段的键_Php_Laravel_Laravel 5 - Fatal编程技术网

如何在PHP中获取动态创建输入字段的键

如何在PHP中获取动态创建输入字段的键,php,laravel,laravel-5,Php,Laravel,Laravel 5,我有多个输入元素,如: <input type="text" name="attribute_name[attr_1]" placeholder="Attribute Name" class="form-control" required=""> <input type="text" name="attribute_name[attr_2]"

我有多个输入元素,如:

<input type="text" name="attribute_name[attr_1]" placeholder="Attribute Name" class="form-control" required="">
<input type="text" name="attribute_name[attr_2]" placeholder="Attribute Name" class="form-control" required="">
..
..

我同意@lagbox,你必须使用属性名称而不是属性名称*

foreach($this->request('attribute_names') as $key => $value) {
    print_r($key);
}

由于属性_在输入框下定义时将自身命名为数组,因此您只需在foreach下使用它,就可以轻松获得每个输入框的键。

动态输入的简短示例:

<form action="" method="post">   
  <input type="text" name="attribute_name[]" value="One">
  <input type="text" name="attribute_name[]" value="Two">
  <input type="text" name="attribute_name[]" value="Three">
  <input type="submit" value="Submit">
</form>
您可以通过
foreach
循环获取所有数据:

foreach($request->attribute_name as $key => $value) {
    echo "Key : " . $key . ", Value : ". $value . "<br>";
}
foreach($request->attribute\u名称为$key=>$value){
回显“键:.$Key.”,值:.$Value.
”; }
输出:

键:1,值:1

键:2,值:2


键:3,值:3

您应该使用名称“attribute\u names”来获取数组而不是“attribute\u name.*”请尝试以下代码

$requestData = $request->input('attribute_names');
foreach($requestData as $key => $val)
 {
      print_r($key);
      print_r($val);
  }

因为输入名为
attribute\u name
而不是
attribute\u name.*
attribute_name
是要迭代的数组本身
foreach($request->attribute_name as $key => $value) {
    echo "Key : " . $key . ", Value : ". $value . "<br>";
}
$requestData = $request->input('attribute_names');
foreach($requestData as $key => $val)
 {
      print_r($key);
      print_r($val);
  }