I have this JSON incoming into a controller method:
$data = $request->get('data');
// output
// [{"key": " Needs Trim ", "value": "Two"}, {"key": "", "value": "empty key"}]
It's an array of objects and I need to to clean it before inserting it to the DB table:
- Trim leading/trailing spaces in key/value (first object)
- Remove any object with an empty key or value from array (second object)
So the final result after the cleaning of the array of objects would look like this:
[{"key": "Needs Trim", "value": "Two"}]
I looked into laravel's array helper functions but I can't seem to get the output I need after hours spent on this. It expects a different format, and their examples show nested arrays as opposed to objects...
Any idea how to accomplish this?