$key is changing on every loop and your check is outside of the loop. Also $contradicts is probably not an array. Check that.
Apr 7, 2020
3
Level 3
in_array key and list of data
so i got a form that need to tick (checkbox). and the tick(checkbox) is an array . am i missing somewhere ?
$is_contradicts = $request->has('contradict');
$contradicts = [];
if($is_contradicts){
$contradicts = $request->get('contradict');
}
foreach ($time_frames as $key => $time_frame){
$analysis = new Analysis([
'stock_id' => $stock->id,
'time_frame' => $time_frame,
'description' => $descriptions[$key],
'is_contradict' => in_array($key,$contradicts) ? 1 : 0
]);
$analysis->save();
}
//example contradicts
0 => '1'
dd(in_array($key,$contradicts); // false -- supposedly true right ??
//result
// db analyses
stock_id | time_frame | description | is_contradict
1 10 Week 0
// expected result
// db analyses
stock_id | time_frame | description | is_contradict
1 10 Week 1
Please or to participate in this conversation.