如何从laravel表单请求获取密钥?

Rub*_*092 9 php laravel laravel-5

我有一个阵列

array:2 [?
  2 => "12"
  7 => "12"
]
Run Code Online (Sandbox Code Playgroud)

从表格发送,我需要2和7如何称呼它们?

键是id的部分,所以想要.foreach获取ID和值然后更新一些东西......

foreach($request->except('_token') as $part) {
    /*get Key here (in this case 2 or 7) and get value here (in this case both 12)*/
}
Run Code Online (Sandbox Code Playgroud)

有人能告诉我怎么做吗?

提前致谢.

Pra*_*man 14

使用$key => $value符号foreach:

foreach ($request->except('_token') as $key => $part) {
  // $key gives you the key. 2 and 7 in your case.
}
Run Code Online (Sandbox Code Playgroud)


Md.*_*leb 7

$data = $request->except('_token')
foreach($data as $id => $value){
    echo "My id is ". $id . " And My value is ". $value;
}
Run Code Online (Sandbox Code Playgroud)


Dan*_*dli 5

用于Collection单行代码而不是foreach().

$requestKeys = collect($request->all())->keys();
Run Code Online (Sandbox Code Playgroud)