2017-04-25 70 views
1

您好,我有以下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

您可以使用收藏。收集($結果) - > GROUPBY( '類'); – yasmuru

+0

謝謝,但不是我想要的,做到這一點,類名成爲一個關鍵,因爲數據是動態生成的,我無法預測關鍵是什麼 – user3718908

回答

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