Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用laravel拆分json_Php_Arrays_Json_Laravel - Fatal编程技术网

Php 如何使用laravel拆分json

Php 如何使用laravel拆分json,php,arrays,json,laravel,Php,Arrays,Json,Laravel,我想拆分json对象我使用的是postman,这是我的body请求,我使用的是laravel框架我是新手,我不知道如何拆分json 这是我实际的json,我想把这个主体请求分成两个对象 我不知道如何在php laravel中做到这一点 { "email": "shahzad@ovadamd.com", "password": "admin123", "password_confirmation": "admin123", "status": 0, "fir

我想拆分json对象我使用的是postman,这是我的body请求,我使用的是laravel框架我是新手,我不知道如何拆分json

这是我实际的json,我想把这个主体请求分成两个对象 我不知道如何在php laravel中做到这一点

{
    "email": "shahzad@ovadamd.com",
    "password": "admin123",
    "password_confirmation": "admin123",
    "status": 0,
    "first_name": "Shahzad",
    "middle_name": "Hussain",
    "last_name": "Shah",
    "date_of_birth": "2015-01-01",
    "gender": "M",
    "area_id": 1,
    "address": "Minhatten NY",
    "city": "New York",
    "state": "Washington",
    "zip": "12312",
    "fax": "111-111-1111",
    "phone_extension": "2471",
    "work_phone": "111-111-1111",
    "phone_no": "111-111-1111",
    "emergency_contact": "111-111-1111",
    "social_security": "111-11-1111",
    "module_id": 1,
    "role_id": 1,
    "speciality_id": 1,
    "facility_id": 1,
    "priv_title": "can edit doctor",
    "priv_key": "ced",
    "display_group": "Doctor",
    "prev_id" :1
}
我想将json拆分为两个对象,如下所示:

{
    "user_profile": {
        "email": "shahzadg@ovadamd.com",
        "password": "admin123",
        "password_confirmation": "admin123",
        "status": 0,
        "first_name": "Shahzad",
        "middle_name": "Hussain",
        "last_name": "Shah",
        "date_of_birth": "2015-01-01",
        "gender": "M",
        "area_id": 1,
        "address": "Minhatten NY",
        "city": "New York",
        "state": "Washington",
        "zip": "12312",
        "fax": "111-111-1111",
        "phone_extension": "2471",
        "work_phone": "111-111-1111",
        "phone_no": "111-111-1111",
        "emergency_contact": "111-111-1111",
        "social_security": "111-11-1111",
        "module_id": 2,
        "role_id": 1
        },

    "prev":{

        "speciality_id": 1,
        "facility_id": 1,
        "priv_title": "can edit doctor",
        "priv_key": "ced",
        "display_group": "Doctor",
        "prev_id" :1
    }
}
非常感谢您的帮助

     $body = $request->all();

    $userProfile = $body['user_profile'];
    $userPrev = $body['prev'];

    $bodyObj = array_merge($userProfile, $userPrev);
    $bodyObj['token'] = $body['token'];

    $validator = UserValidations::validateUser($request->all());


           public function register(Request $request) {
    $body = $request->all();

    $userProfile = $body['user_profile'];
    $userPrev = $body['prev'];

    $bodyObj = array_combine($userProfile, $userPrev); array_chunk($body,true);
    $bodyObj['token'] = $body['token'];

    $validator = UserValidations::validateUser($bodyObj);

    if ($validator->fails()) {
        return response([
            'status' => false,
            'message'   => __('messages.validation_errors'),
            'errors' => $validator->errors()->all()
        ], 200);
    }

    DB::beginTransaction();

    try{

        $token = JWTAuth::getToken();
        $apy = JWTAuth::getPayload($token)->toArray();
        $request->merge(['module_id' => $apy['module_id']]);
        $request->request->add(['created_by' => Auth::user()->id]);
        $request->merge(['password' =>  bcrypt($request->input('password'))]);
        $user = $this->user->create($request->only($this->user->getModel()->fillable));
        $request->request->add(['user_id' => $user->id]);
        $this->userBasicInfo->create($request->only($this->userBasicInfo->getModel()->fillable));
        $this->userContactDetails->create($request->only($this->userContactDetails->getModel()->fillable));
        $this->userAccessModule->create($request->only($this->userAccessModule->getModel()->fillable));
        $this->userRoles->create($request->only($this->userRoles->getModel()->fillable));
        $this->verifyUser->create(['user_id' => $user->id, 'token' => str_random(40)]);

        $this->userSpeciality->create($request->only($this->userSpeciality->getModel()->fillable));
        $this->userFacility->create($request->only($this->userFacility->getModel()->fillable));
        $this->userDefinition->create($request->only($this->userDefinition->getModel()->fillable));
        $this->userPrev->create($request->only($this->userPrev->getModel()->fillable));


        Mail::to($user->email)->send(new VerifyMail($user));

        DB::commit();

        return response([
            'status' => true,
            'message' => 'User registered successfully',
        ], 200);

    } catch(\Exception $ex) {
        DB::rollback();
        return response([
            'status' => false,
            'message' => __('messages.validation_errors'),
            'errors' => $ex->getMessage(),
        ], 500);
    }
}

将字符串解码为一个数组,并使用array\u chunk将其拆分。 不要忘记第三个参数来保留关键点。 然后使用array_combine使数组关联

$arr = array_combine(["user_profile", "prev"], array_chunk(json_decode($json, true),22, true));
var_dump($arr);
返回:

array(2) {
  ["user_profile"]=>
  array(22) {
    ["email"]=>
    string(19) "shahzad@ovadamd.com"
    ["password"]=>
    string(8) "admin123"
    ["password_confirmation"]=>
    string(8) "admin123"
    ["status"]=>
    int(0)
    ["first_name"]=>
    string(7) "Shahzad"
    ["middle_name"]=>
    string(7) "Hussain"
    ["last_name"]=>
    string(4) "Shah"
    ["date_of_birth"]=>
    string(10) "2015-01-01"
    ["gender"]=>
    string(1) "M"
    ["area_id"]=>
    int(1)
    ["address"]=>
    string(12) "Minhatten NY"
    ["city"]=>
    string(8) "New York"
    ["state"]=>
    string(10) "Washington"
    ["zip"]=>
    string(5) "12312"
    ["fax"]=>
    string(12) "111-111-1111"
    ["phone_extension"]=>
    string(4) "2471"
    ["work_phone"]=>
    string(12) "111-111-1111"
    ["phone_no"]=>
    string(12) "111-111-1111"
    ["emergency_contact"]=>
    string(12) "111-111-1111"
    ["social_security"]=>
    string(11) "111-11-1111"
    ["module_id"]=>
    int(1)
    ["role_id"]=>
    int(1)
  }
  ["prev"]=>
  array(6) {
    ["speciality_id"]=>
    int(1)
    ["facility_id"]=>
    int(1)
    ["priv_title"]=>
    string(15) "can edit doctor"
    ["priv_key"]=>
    string(3) "ced"
    ["display_group"]=>
    string(6) "Doctor"
    ["prev_id"]=>
    int(1)
  }
}
一旦数组被拆分,您就可以使用Json_encode轻松地再次将其转换为Json。

我做过类似的事情,请看我的编辑问题。我不明白这段代码与拆分json字符串有什么关系。您是否尝试了我发布的代码?json_decode预期参数1为字符串,数组在我尝试您codecan时出现此错误,我们已经聊天了?code experts$json为json字符串,您在问题中遇到的字符串