doujie3888 2015-07-17 07:04
浏览 31
已采纳

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

For this particular problem I need to send 2 serialized forms to my server at once plus a few values in JSON format. The structure of my JSON object will always look like the below:

"{  
  "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="
 ]}"

I then access the object with the below:

$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'];

So now the variable $workoutDaysArray, for instance hold the value of a serialized form represented as a string. I'm hoping there's a way to access the key value pairs in that serialized form without having to parse the string manually or rewrite a lot of javascript.

I was hoping something similar to this would work, but I think I may be off here:

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

Any thoughts?

Thanks!!

  • 写回答

1条回答 默认 最新

  • duanchuonong5370 2015-07-17 07:26
    关注

    Take a look with var_dump at your result:

    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="
      }
    }
    

    So, client_update_form is a string and looks like url parameters.

    Use parse_str() to parse the string into an array:

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

    Result:

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

    So, your value is now in $client_update_form['client_id'].

    Complete code:

    $input = json_decode( file_get_contents('php://input') );
    parse_str( $input->client_update_form, $client_update_form );
    echo $client_update_form['client_id'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程