如何从JSON删除索引-使用Laravel

时间:2019-04-03 09:21:36

标签: arrays json laravel

当前我有以下json响应:

"privileges": {
  "0": {
    "is_super_admin": 1
  },
  "facilities": {
    "facility_id": 1,
    "speciality_id": 1,
    "is_facility_supervisor": 1
  },
  "priv_key": "ced"
}

但是我想要的响应如下:

"privileges":{

  "is_super_admin": 1,

  "facilities": [
    {
      "facility_id": 1,
      "is_facility_supervisor": 1,
      "speciality_id": 1,
      "prev_key": "ced"
    },

  ]

}

这是我的控制器代码:

$is_super_admin = DB::table('users')->select('users.is_super_admin')->where('id',$currentUser->id)->first();

$speciality_id = DB::table('user_facility')->select('user_facility.facility_id','user_facility.speciality_id','user_facility.is_facility_supervisor')->where('user_id',$currentUser->id)->first();

$priv_key = DB::table('prev_definition')->where('user_id',$currentUser->id)->pluck('priv_key')->first();

$superadmin = (object) $is_super_admin;
$specialities = (object) $speciality_id;


$response = [
    $superadmin,
    'facilities' => $specialities,
    'priv_key' => $priv_key
];

我被困在如何获得所需的响应中,我希望在我的设施对象中使用“ priv_key”:“ ced”,在特权对象中使用“ is_super_admin”:1并删除索引0,

我们将非常感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

您可以通过调整以下对象的结构来调整最终结果:

$is_super_admin = DB::table('users')->select('users.is_super_admin')->where('id',$currentUser->id)->first();

$speciality_id = DB::table('user_facility')->select('user_facility.facility_id','user_facility.speciality_id','user_facility.is_facility_supervisor')->where('user_id',$currentUser->id)->first();

$priv_key = DB::table('prev_definition')->where('user_id',$currentUser->id)->pluck('priv_key')->first();

$superadmin = $is_super_admin->is_super_admin;
$specialities = (object) $speciality_id;
$specialities->priv_key = $priv_key;

$response = ['is_super_admin' => $superadmin, 'facilities' => [$specialities]];