doujia2386 2009-09-24 22:25
浏览 62
已采纳

将具有相同变量名的多个GET值组合成一个字符串

I have a form which sends lots of GET['var'] in a form.

Ive worked out how to set it to send var[1]=data&var[2]=some&var[3]=thing etc etc

but how do I catch each variable and combine it into one string, seperated with a ", "?

so $var = data, some, thing

UPDATE:

Sorry I should of mentioned I already have the function that implodes the string but I dont know how to combine all the Var[n]'s into one $var string.

Hope this makes sense!

Solved!

Thanks Kazar, your first answer actually worked a treat! it required me altering my script a little but your way certainly makes sense to me now

  • 写回答

2条回答 默认 最新

  • dqpciw9742 2009-09-24 22:27
    关注

    The php implode function will concatenate the contents of an array together, using a string to insert between them, like so:

    $var = implode(',', $_GET['var']);
    

    An additional note, the order in which the various elements of var appear does depend on the order in the querystring, so a string could come out looking like 'var2,var0,var1'. To get round this, you may want to do a key sort first:

    $var = $_GET['var'];
    ksort($var);
    $joinedString = implode(',', $var);
    

    Edit: According to the question edit:

    Assuming you mean to create the query string again...?

    $var = $_GET['var'];
    $components = array();
    
    foreach($var as $key=>$value) {
    
      $components[] = "var[" . $key . "]=" . $value;
    
    }
    
    $string = implode('&', $components);
    

    Might help to have a more detailed description.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c