首页 > 解决方案 > How do I loop a nested array in laravel to get an array based on conditions of key values from that array

问题描述

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

标签: phplaravelmultidimensional-array

解决方案



推荐阅读