douqin6785 2016-07-26 11:15
浏览 165
已采纳

将表单提交到JSON文件时保留数组

I have a form that takes in data and writes to a JSON form in PHP.

I needed to submit an array as a numeric input but it keeps giving me a string. Is it possible to enable the form to submit as an array via text input box?

Form example:

<input type="text" name="arraytobepushed[]" placeholder="EG: 1000,2000,3000" />

The output is:

{
"obj": [{
   "arraytobepushed": ["1000,2000,3000"]
       }]
}
  • 写回答

2条回答 默认 最新

  • dongyaofu0599 2016-07-26 11:26
    关注

    You could turn the text into an array by using explode() So you would have something like this:

    <?PHP
      $myArray = explode(',', $_POST['arraytobepushed[]']);
    ?>
    

    The explode() function splits everything separated by the first argument (in this case a comma) you pass and puts it into an array.

    So if your inputted was 1000, 2000, 3000 your $myArray would look like:

    index 0 = "1000" ($myArray[0])

    index 1 = "2000" ($myArray[1])

    index 2 = "3000" ($myArray[2])

    Keep in mind that the values are still strings, not integers. If you want to make them integers you can do this:

    $myArray = array_map('intval', explode(',', $_POST['arraytobepushed[]'])); 
    

    This makes all your elements into integers like so:

    index 0 = 1000 ($myArray[0])

    index 1 = 2000 ($myArray[1])

    index 2 = 3000 ($myArray[2])

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

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题