dongxinm279890 2011-07-27 20:08
浏览 67
已采纳

如何通过表单提交传递数组值?

I am trying to pass an array of values through a form submission. As an example:

example.com?value1=178&value2=345&value3=2356

The easy solution would be for me to do the following to get the values on the new page:

$value1=$_GET['value1'];
$value2=$_GET['value2'];
$value3=$_GET['value3'];

The difficulty that I am having is that the variable after the word 'value' passed through the form will change with each submission. So I have amended the code to pass as:

example.com?value14=178&variable=14&value23=345&variable=23&value63=2356&variable=63

As you can see here, I have now passed the variable that comes in from of the value as a GET parameter. My attempt then GET these values to display individually on the submitted page is as follows:

$variable=$_GET['variable'];    
$value=$_GET['value'.$variable];

echo $value . '<br>';

This code almost works. I am able to get the last array which is passed through to display. How can I fix this code to get all of the passed values to display on the submitted page?

  • 写回答

3条回答 默认 最新

  • dongmin3754 2011-07-27 20:13
    关注

    Use PHP's array notation for form fields:

    val[]=178&val[]=14&val[]=345&etc...
    

    This will cause $_GET['val'] to be an array:

    $_GET = array(
       'val' => array(178, 14, 345, etc...)
    )
    

    If you can't rearrange the URL like that, you can try using preg_grep:

    $matches = preg_grep('/^variable\d+$/', array_keys($_GET));
    

    which'll return :

    $matches= array('variable1', 'variable2', 'variable3', etc...);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码