我如何在PHP中访问JSON对象中序列化HTML表单的值?

时间:2015-07-17 07:04:14

标签: javascript php json

对于这个特殊问题,我需要一次向我的服务器发送2个序列化表格加上一些JSON格式的值。我的JSON对象的结构总是如下所示:

"{  
  "startDate":"2015-07-20",
  "planName":"MySecondAttempt",
  "clientId":"5",
  "client_update_form":"client_id=5&squat_max=300&deadlift_max=100&bench_max=275",
  "user_id":3,
  "workoutDaysArray":[  
  "workoutDayName=Legs&copyUnderway=0&date=July+21&workoutId=4&setInstruction1=2&repInstruction1=4&weightInstruction1=6&restInstruction1=8&workoutId2=&setInstruction2=&repInstruction2=&weightInstruction2=&restInstruction2=",
  "workoutDayName=Arms&copyUnderway=0&date=July+23&workoutId=7&setInstruction1=1&repInstruction1=2&weightInstruction1=3&restInstruction1=4&workoutId2=2&setInstruction2=2&repInstruction2=4&weightInstruction2=6&restInstruction2=8&workoutId3=3&setInstruction3=2&repInstruction3=4&weightInstruction3=6&restInstruction3=8&workoutId4=&setInstruction4=&repInstruction4=&weightInstruction4=&restInstruction4="
 ]}"

然后我使用以下内容访问该对象:

$input = json_decode(file_get_contents('php://input'), true);
$trainerId          = $input['user_id'];
$workoutDaysArray   = $input['workoutDaysArray'];
$startDate          = $input['startDate'];
$planName           = $input['planName'];
$clientUpdateForm   = $input['client_update_form'];

所以现在变量$ workoutDaysArray,例如保存表示为字符串的序列化表单的值。我希望有一种方法可以访问该序列化表单中的键值对,而无需手动解析字符串或重写大量的javascript。

我希望类似的东西能起作用,但我想我可能会离开这里:

foreach($workoutDaysArray as $key=>$value){
   //some code
}

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

在你的结果中查看var_dump:

object(stdClass)#1 (6) {
  ["startDate"]=>
  string(10) "2015-07-20"
  ["planName"]=>
  string(15) "MySecondAttempt"
  ["clientId"]=>
  string(1) "5"
  ["client_update_form"]=>
  string(56) "client_id=5&squat_max=300&deadlift_max=100&bench_max=275"
  ["user_id"]=>
  int(3)
  ["workoutDaysArray"]=>
  array(2) {
    [0]=>
    string(219) "workoutDayName=Legs&copyUnderway=0&date=July+21&workoutId=4&setInstruction1=2&repInstruction1=4&weightInstruction1=6&restInstruction1=8&workoutId2=&setInstruction2=&repInstruction2=&weightInstruction2=&restInstruction2="
    [1]=>
    string(397) "workoutDayName=Arms&copyUnderway=0&date=July+23&workoutId=7&setInstruction1=1&repInstruction1=2&weightInstruction1=3&restInstruction1=4&workoutId2=2&setInstruction2=2&repInstruction2=4&weightInstruction2=6&restInstruction2=8&workoutId3=3&setInstruction3=2&repInstruction3=4&weightInstruction3=6&restInstruction3=8&workoutId4=&setInstruction4=&repInstruction4=&weightInstruction4=&restInstruction4="
  }
}

因此,client_update_form是一个字符串,看起来像url参数。

使用parse_str()将字符串解析为数组:

parse_str( $input->client_update_form, $client_update_form );

结果:

array(4) {
  ["client_id"]=>
  string(1) "5"
  ["squat_max"]=>
  string(3) "300"
  ["deadlift_max"]=>
  string(3) "100"
  ["bench_max"]=>
  string(3) "275"
}

因此,您的值现在位于$client_update_form['client_id']

完整代码:

$input = json_decode( file_get_contents('php://input') );
parse_str( $input->client_update_form, $client_update_form );
echo $client_update_form['client_id'];