在Laravel中,有什么好方法可以在对象数组中删除对象属性并删除请求中不必要的属性



{
"city_name": "new cityName",
"country_id": 3,
"localities": [
{
"name": "locality 11",
"postal-code": 123456,
"locality-specialized-in": "FOOD,TECH",
"extra_data" : "unwanted, unnecessary data"
},
{
"name": "locality 21",
"postal-code": "1234567",
"extra_data" : "unwanted, unnecessary data",
"extra_data_two" : "unwanted, unnecessary data"
}
]
}

在上面的请求中,只想用字段(namepostal-code(localities,并想删除多余的字段(如上述请求locality-specialized-in(。 只是没有使用循环或任何好的方式

来自上述请求的预期过滤器数据

$localities_array = [
[
"name"=> "locality 11",
"postal-code"=> 123456
],
[
"name"=> "locality 21",
"postal-code"=> 1234567
]
]

目前,我在控制器中使用以下方法来存档。

$localities = null;
foreach (request()->get('localities') as $locality) {
$localities[] = ["name"=>$locality['name'],"postal-code"=>$locality['postal-code'] ];
}

建议不胜感激。

如果要从请求中的数组中获取值,请使用以下代码。

$localities = null;
$localities['name'] = request()->get('localities.name');
$localities['postal-code'] = request()->get('localities.postal-code');

相关内容

  • 没有找到相关文章

最新更新