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'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?