Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ethar's avatar
Level 5

update array

i have this values come from form

"explainanswer": {
"53": "yes php framework ",
"54": "not javascript ",
"55": "not security",
"56": "not forensics "
},
"answer": {
"53": "php framework",
"54": "javascript",
"55": "security",
"56": "forensics"
},

i have table answers with this fields

`id`,`answer`, `explain_answer`,

how can update this fields depend on id , id come from table as above 53,54,55,56

i try this code but not work

        foreach ($request->answer as $key=>$val){
            $answer=Answer::findOrFail($key);
            $answer->answer = $request->answer[$key];
            $answer->explain_answer = $request->explainanswer[$key];
            $answer->save();
        }
0 likes
2 replies
laracoft's avatar

/neederrorlog

Please elaborate "not work". What is the error or exception? Not work does not tell us what is wrong.

jlrdw's avatar

That doesn't look like a valid Json, this would be valid:

{
	"explainanswer": {
		"53": "yes php framework",
		"54": "not javascript",
		"55": "not security",
		"56": "not forensics "
	},
	"answer": {
		"53": "php framework",
		"54": "javascript",
		"55": "security",
		"56": "forensics"
	}
}

Would probably be easier to Json decode then loop.

https://gist.github.com/jimgwhit/cbbe5bb0d2556fdc7e37a86d3630239c just example.

Please or to participate in this conversation.