普通网友 2017-03-04 05:36
浏览 516
已采纳

Laravel:循环遍历数组并将其转换为修改后的数组,其中包含一个键中的原始数组

I have a data array that contains food items and looks like this.

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

]

Now I have to loop through that array and if the name has either SMALL, MEDIUM, or LARGE I have to reformat the data so that it should look something like this e.g.

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

The legacy system of the client was designed poorly and they are requiring an api that properly formats the data into a more granularized format. I was trying to figure out as to how do this. Please take note that the original array has more items than my example and I should loop through each one of them. Should I rebuild the data from scratch? Or is there a better way of doing this while looping through the array?

  • 写回答

2条回答 默认 最新

  • doushou1298 2017-03-04 10:24
    关注

    You could use Collections and do something like:

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

    If you would prefer something a little shorter for the last map you could inline it :

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

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)