分组对象数组

时间:2017-04-25 10:54:44

标签: laravel eloquent transformer

您好我有以下JSON:

[
  {
    "id": 1,
    "company_id": 2,
    "route_id": 16,
    "class": "Standard",
    "fare": 35,
    "weekday": "monday",
    "reporting_time": "06:00 PM",
    "departure_time": "07:00 PM",
    "extras": []
  },
  {
    "id": 2,
    "company_id": 2,
    "route_id": 16,
    "class": "Standard",
    "fare": 35,
    "weekday": "tuesday",
    "reporting_time": "06:00 PM",
    "departure_time": "07:00 PM",
    "extras": []
  },
  {
    "id": 3,
    "company_id": 2,
    "route_id": 16,
    "class": "Standard",
    "fare": 35,
    "weekday": "wednesday",
    "reporting_time": "06:00 PM",
    "departure_time": "07:00 PM",
    "extras": []
  },
  {
    "id": 4,
    "company_id": 2,
    "route_id": 16,
    "class": "VIP",
    "fare": 35,
    "weekday": "thursday",
    "reporting_time": "06:00 PM",
    "departure_time": "07:00 PM",
    "extras": []
  },
  {
    "id": 5,
    "company_id": 2,
    "route_id": 16,
    "class": "VIP",
    "fare": 35,
    "weekday": "friday",
    "reporting_time": "06:00 PM",
    "departure_time": "07:00 PM",
    "extras": []
  },
  {
    "id": 6,
    "company_id": 2,
    "route_id": 16,
    "class": "Business",
    "fare": 35,
    "weekday": "saturday",
    "reporting_time": "06:00 PM",
    "departure_time": "07:00 PM",
    "extras": []
  }
]

这是我的雄辩模型返回数据的方式,我用变换器格式化输出以隐藏某些字段等。

我想通过class属性对此数据进行分组。这就是我希望数据的样子:

[
   {
      "class":"Standard",
      "schedules":[
         {
            "id":1,
            "company_id":2,
            "route_id":16,
            "fare":35,
            "weekday":"monday",
            "reporting_time":"06:00 PM",
            "departure_time":"07:00 PM",
            "extras":[

            ]
         },
         {
            "id":2,
            "company_id":2,
            "route_id":16,
            "fare":35,
            "weekday":"tuesday",
            "reporting_time":"06:00 PM",
            "departure_time":"07:00 PM",
            "extras":[

            ]
         },
         {
            "id":3,
            "company_id":2,
            "route_id":16,
            "fare":35,
            "weekday":"wednesday",
            "reporting_time":"06:00 PM",
            "departure_time":"07:00 PM",
            "extras":[

            ]
         }
      ]
   },
   {
      "class":"VIP",
      "schedules":[
         {
            "id":4,
            "company_id":2,
            "route_id":16,
            "fare":35,
            "weekday":"thursday",
            "reporting_time":"06:00 PM",
            "departure_time":"07:00 PM",
            "extras":[

            ]
         },
         {
            "id":5,
            "company_id":2,
            "route_id":16,
            "fare":35,
            "weekday":"friday",
            "reporting_time":"06:00 PM",
            "departure_time":"07:00 PM",
            "extras":[

            ]
         }
      ]
   },
   {
      "class":"Business",
      "schedules":[
         {
            "id":6,
            "company_id":2,
            "route_id":16,
            "fare":35,
            "weekday":"saturday",
            "reporting_time":"06:00 PM",
            "departure_time":"07:00 PM",
            "extras":[

            ]
         }
      ]
   }
]

如何在不改变雄辩查询或数据库结构的情况下执行此操作?有没有办法可以用变压器实现这个目标?如果没有,我唯一的选择就是更改雄辩的查询,我该怎样做才能让数据看起来像我想要的那样?

1 个答案:

答案 0 :(得分:2)

    $result=[];
    foreach($youarray as $key => $item)
    {
       $result[ $item['class'] ]['class'] = $item['class'];
       $result[ $item['class'] ]['schedules'] = [$item['id'], $item['company_id'], etc. ];
    } 
    return $result;