2017-03-04 5 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; 
関連する問題