2017-03-04 1 views
0

나는 음식 아이템을 포함하고있는 데이터 배열을 가지고있다.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; 
    }); 

:

+3

대용량 데이터의 루프를 사용하여 데이터를 다시 처리하고 다시 포맷하는 대신, 사용자의 편의에 따라 원래의 어레이를 재구성하는 것이 좋습니다. –

+0

어떤 Laravel 버전을 사용하고 있습니까? –

+0

@RossWilson Laravel 5.1 –

답변

0

당신은 Collections를 사용하고 같은 뭔가를 할 수

->map(function ($items) { 
    return $items->count() < 2 ? $items : $items->first()->except('price')->put('itemModifiers', $items); 
}); 

희망이 도움이됩니다!

0
{"FRENCH FRIES":[{"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"}],"CRINCKLE WEDGES":[{"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}]} 

코드

$original = json_decode($original); 
$output = array(); 
foreach ($original as $key => $row) { 
    $newName = str_replace('SMALL', '', $row->name); 
    $newName = str_replace('MEDIUM', '', $newName); 
    $newName = str_replace('LARGE', '', $newName); 
    $newName = trim($newName); 
    if(in_array($newName,$output)){ 
    array_push($test[$newName], $row); 
    }else{ 
    array_push($output, $newName); 
    $test[$newName] = array(); 
    array_push($test[$newName], $row); 
    } 

} 
echo "<h3>Output</h3><pre>"; print_r(json_encode($test)); exit;