I want to get only the arrays where the option is not null When i get the arrays back successfully, i also want to add it to a database, but most importantly i want to get the arrays back
array:3 [
0 => array:5 [
"id" => 6
"option" => "True"
"is_correct" => true
"label" => "True"
"opid" => 1
]
1 => array:5 [
"id" => 7
"option" => "False"
"is_correct" => false
"label" => "False"
"opid" => 1
]
2 => array:5 [
"id" => 8
"option" => null
"is_correct" => false
"label" => "Theory"
"opid" => 5
]
]
EXPECTED RESULT
array:3 [
0 => array:5 [
"id" => 6
"option" => "True"
"is_correct" => true
"label" => "True"
"opid" => 1
]
1 => array:5 [
"id" => 7
"option" => "False"
"is_correct" => false
"label" => "False"
"opid" => 1
]
]
WHAT I HAVE TRIED
foreach($request->option as $option) {
if($option["option"] == null){
dd($option)
}
}
i only get the first value from the array.. it appears like my if condition is not working as well
i'll be in the comments thank you
Try except
method,
Read documentation - https://laravel.com/docs/8.x/eloquent-collections#method-contains
Suppose if your $request->option
contain array then
$filtered = collect($request->option)->whereNotNull('option')->all()->toArray();
Ref: https://laravel.com/docs/8.x/collections#method-wherenotnull
Use $z
variable, it contains the array as per your requirement.
Demo: https://paiza.io/projects/FTTBeEEnb-EHHJbU8FOAYw
$x = [
[
"id" => 6,
"option" => "True",
"is_correct" => true,
"label" => "True",
"opid" => 1
],
[
"id" => 7,
"option" => "False",
"is_correct" => false,
"label" => "False",
"opid" => 1
],
[
"id" => 8,
"option" => null,
"is_correct" => false,
"label" => "Theory",
"opid" => 5
]
];
$z = [];
foreach ($x as $y) {
if ($y['option'] != null) {
array_push($z, $y);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.