나는 음식 아이템을 포함하고있는 데이터 배열을 가지고있다.Laravel : 배열을 반복하면서 원래 배열을 키로 수정 한 배열로 변환
[
{
"itemId": 80001,
"name": "FRENCH FRIES SMALL",
"description": "More delicious than ever, our signature piping hot, thick cut Salted French Fries are golden on the outside and fluffy on the inside.",
"price": 6,
"slug": "french-fries-small-80001"
},
{
"itemId": 80002,
"name": "FRENCH FRIES MEDIUM",
"description": "More delicious than ever, our signature piping hot, thick cut Salted French Fries are golden on the outside and fluffy on the inside.",
"price": 7,
"slug": "french-fries-medium-80002"
},
{
"itemId": 80003,
"name": "FRENCH FRIES LARGE",
"description": "More delicious than ever, our signature piping hot, thick cut Salted French Fries are golden on the outside and fluffy on the inside.",
"price": 8,
"slug": "french-fries-large-80003"
},
{
"itemId": 80052,
"name": "CRINCKLE WEDGES SMALL",
"description": "CRINCKLE WEDGES SMALL",
"price": 7,
"slug": "crinckle-wedges-small-80052",
"sequence": 14
},
{
"itemId": 80053,
"name": "CRINCKLE WEDGES MEDIUM",
"description": "CRINCKLE WEDGES MEDIUM",
"price": 8,
"slug": "crinckle-wedges-medium-80053",
"sequence": 15
},
{
"itemId": 80054,
"name": "CRINCKLE WEDGES LARGE",
"description": "CRINCKLE WEDGES LARGE",
"price": 9,
"slug": "crinckle-wedges-large-80054",
"sequence": 16
},
]
는 지금은 그 배열을 통해 루프가 있고 이름이 SMALL, MEDIUM 또는 LARGE 중 하나가있는 경우는이 예를 들어, 같은 것을 보일 것 있도록 나는 데이터를 포맷해야
{
"itemId": 80001,
"name": "FRENCH FRIES",
"description": "More delicious than ever, our signature piping hot, thick cut Salted French Fries are golden on the outside and fluffy on the inside.",
"itemModifiers":[
{
"itemId": 80001,
"name": "FRENCH FRIES SMALL",
"description": "More delicious than ever, our signature piping hot, thick cut Salted French Fries are golden on the outside and fluffy on the inside.",
"price": 6,
"slug": "french-fries-small-80001"
},
{
"itemId": 80002,
"name": "FRENCH FRIES MEDIUM",
"description": "More delicious than ever, our signature piping hot, thick cut Salted French Fries are golden on the outside and fluffy on the inside.",
"price": 7,
"slug": "french-fries-medium-80002"
},
{
"itemId": 80003,
"name": "FRENCH FRIES LARGE",
"description": "More delicious than ever, our signature piping hot, thick cut Salted French Fries are golden on the outside and fluffy on the inside.",
"price": 8,
"slug": "french-fries-large-80003"
}
],
"slug": "french-fries-80001",
"sequence": 8
}
클라이언트의 레거시 시스템이 제대로 디자인 된 그들은 제대로 더 granularized 형식으로 데이터를 포맷하는 API를 요구하고있다. 나는 이것을 어떻게하는지 알아 내려고 노력했다. 원래 배열에는 예제보다 많은 항목이 있으며 각 항목을 반복해야합니다. 데이터를 처음부터 다시 작성해야합니까? 또는 배열을 반복하면서이 작업을 수행하는 더 좋은 방법이 있습니까? 마지막 map
에 대한 약간 짧은 것을 선호하는 경우에 당신이 그것을 인라인 수
$data = collect(json_decode($data, true))
->map(function ($item) {
return collect($item);
})
->groupBy(function ($item) {
return trim(str_replace(['SMALL', 'MEDIUM', 'LARGE'], '', $item['name']));
})
->map(function ($items) {
if ($items->count() > 2) {
return $items;
}
$parent = $items->first()->except('price');
$parent->put('itemModifiers', $items);
return $parent;
});
:
대용량 데이터의 루프를 사용하여 데이터를 다시 처리하고 다시 포맷하는 대신, 사용자의 편의에 따라 원래의 어레이를 재구성하는 것이 좋습니다. –
어떤 Laravel 버전을 사용하고 있습니까? –
@RossWilson Laravel 5.1 –