2017-03-04 26 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 
         }, 
        ] 

现在我通过阵列必须循环,如果名下有两种小型,中型或大型我必须重新格式化数据,因此它应该是这个样子例如

{ 
         "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 
        } 

客户的遗留系统设计不当,他们被要求适当的数据格式为更颗粒化格式的API。我试图弄清楚如何做到这一点。请注意,原始数组比我的例子有更多的项目,我应该循环每一个。我应该从头重建数据吗?或者有更好的方式来循环访问数组?

+3

取而代之的是对数据进行额外处理,并且每次使用循环对大量数据进行重新格式化,如果根据您的方便重新构建原始数组,那听起来会更好。 –

+0

您使用的是什么版本的Laravel? –

+0

@RossWilson Laravel 5.1 –

回答

0

你可以使用Collections,做一些事情,如:

$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; 
    }); 

,如果您喜欢的东西略短一些过去map你可以内嵌它:

->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;