Jordy Groote Jordy Groote - 7 days ago 6
PHP Question

How to get the key from a laravel form request?

I have an array

array:2 [▼
2 => "12"
7 => "12"
]


Send from a form, I need the 2 and 7 how do i call them?

The keys are id's of parts so want to.
foreach get the ID and value
then update something...

foreach($request->except('_token') as $part) {

get Key here (in this case 2 or 7) and get value here (in this case both 12)

}


Can somebody tell me how to do this?

Thanks in advance.

Answer

Use $key => $value notation in foreach:

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