1

I've run into a specific problem, where I need to remove specific nested array key from my request, so it doesn't store in my database. I created a Request class, and this is how I tried to accomplish this so far:

This is my data that I'm getting from request after form submit:

  "options" => array:2 [▼
    "product_options" => array:1 [▶]
    "additional_product_options" => array:1 [▼
      "Tenetur dolor labore" => null
    ]
  ]

As you can see, additional_product_options array has a key with an empty value, and so I want to remove this key from array completely. I tried this using both unset() and array_filter(), but I'm not getting desired result. This is how I tried it so far:

if(isset($this->input('options')['additional_product_options'])){
            foreach ($this->input('options')['additional_product_options'] as $key => $val){
                if(is_null($key) || is_null($val)){ //Check for null values
                    unset($this->input('options')['additional_product_options'][$key]); //Remove key from array
                }
            }
        }

After I dump my request with dd($this->options()), I'm still getting Tenetur dolor labore key in this array:

array:2 [▼
  "product_options" => array:1 [▼
    "Et sit id culpa rep" => "sdad,ads"
  ]
  "additional_product_options" => array:1 [▼
    "Tenetur dolor labore" => null
  ]
]

With array_filter(), I'm still getting the same results:

if(isset($this->input('options')['additional_product_options'])){
     array_filter($this->input('options')['additional_product_options']);
}

After dumping my data, the key is still here:

  "options" => array:2 [▼
    "product_options" => array:1 [▶]
    "additional_product_options" => array:1 [▼
      "Tenetur dolor labore" => null
    ]
  ]

In ideal case,the Tenetur dolor labore key would be removed completely, and additional_product_options array would be empty. What am I doing wrong here?

5
  • Just to check $this->input() this is a method that returns a value. So each time you call it then will this be a new value or does it set the value back to wherever it gets the data from? Commented May 28, 2020 at 19:44
  • array_filter does not remove data, it filters out data from a specific array. Commented May 28, 2020 at 20:28
  • @NigelRen The $this->input() actually returns all the data that was sent when the form was submitted, so it will change when the form data changes. Commented May 29, 2020 at 6:34
  • My point is that you don't actually store this data and manipulate the value, you constantly retrieve it and change the return from it. Inside the if, something like $options = $this->input('options')['additional_product_options']; and then process $options instead. Commented May 29, 2020 at 6:37
  • I get what you're saying now, will try to do that instead. Commented May 29, 2020 at 6:48

1 Answer 1

1

try this ... i have test it for current options array:

for test reason, i made the option array:

   $options = ['product_options' => ['additional_product_options' => 
['Tenetur dolor labore' => null]], 'additional_product_options' => 'some value'];

then

  unset($options['product_options']['additional_product_options']['Tenetur dolor labore']);

the result is removing the key as we want ...

Sign up to request clarification or add additional context in comments.

1 Comment

Hmm, this will work, but I will get different additional_product_options keys every time I submit a new form, so this would have to be more dynamic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.