2017-04-12 41 views
0

我要變換的JSON數據我越來越並將其發送給請求的應用程序,使他們能夠在我的控制器輕鬆獲得鍵值變換JSON數據索引和發送

//this is what i'm getting in postman 

{ 
    "dropdown_data": { 
     "driving_regions": [{ 
      "name": "East India" 
     }, { 
      "name": "West India" 
     }], 
     "employment_types": [{ 
      "type": "Owner" 
     }, { 
      "type": "Other" 
     }], 
     "event_names": [{ 
      "name": "Transport Mitra" 
     }, { 
      "name": "Other" 
     }], 
     "languages": [{ 
      "name": "English" 
     }, { 
      "name": "Spanish" 
     }, { 
      "name": "Hindi" 
     }] 
    }, 
    "user_details": { 

    } 
} 

//i want to transform like the below 

{ 
    "0": "dropdown_data", 
    "dropdown_data": { 
     "0": "driving_regions", 
     "driving_regions": [{ 
      "name": "East India" 
     }, { 
      "name": "West India" 
     }], 
     "1": "employment_types", 
     "employment_types": [{ 
      "type": "Owner" 
     }, { 
      "type": "Other" 
     }], 
     "2": "event_names", 
     "event_names": [{ 
      "name": "Transport Mitra" 
     }, { 
      "name": "Other" 
     }], 
     "3": "languages", 
     "languages": [{ 
      "name": "English" 
     }, { 
      "name": "Spanish" 
     }, { 
      "name": "Hindi" 
     }] 
    }, 
    "1": "user_details", 
    "user_details": { 

    } 
} 

我是剛剛起步的數據和變量sotring然後i'l只是返回的JSON如下圖所示

return Response::json([ 
     'dropdown_data' => [ 
      'castes' => $castes->all(), 
      'driving_regions' => $driving_regions->all(), 
      'employment_types' => $employment_types->all(), 
      'event_names' => $event_types->all(), 
      'languages' => $languages->all() 
     ], 
      'user_details' => $user_details, 
    ]); 

謝謝

回答

0

它們轉換成對象:

return Response::json([ 
    'dropdown_data' => [ 
     'castes' => (object) $castes->all(), 
     'driving_regions' => (object) $driving_regions->all(), 
     'employment_types' => (object) $employment_types->all(), 
     'event_names' => (object) $event_types->all(), 
     'languages' => (object) $languages->all() 
    ], 
     'user_details' => $user_details, 
]);