dongzhen6554 2013-12-11 16:49
浏览 22
已采纳

将$ _POST文本输入转换为多维数组

On my online shop, there's a form with a big number of text inputs, representing quantities of products. There are three fields for every product: quantity of product in S size, M size and L size.

So the name of input field must contain both the product id and the size. This is simplified code of the form:

foreach ($productid as $id ) {
    echo '<input type="text" name="s'.$id.'" />';
    echo '<input type="text" name="m'.$id.'" />';
    echo '<input type="text" name="l'.$id.'" />';
}

I want to process this input sent via $_POST and save all values of all input field into a single multidimensional array. The format of the desired array is as follows:

$input['32']['m']='20' means that the customer ordered 20x the product of id 32 in size M.

All my tries to do that failed because I don't understand how to loop through $_POST values to turn them into more than a one-dimension array.

Any idea how to process such input and get a two-dimension array?

  • 写回答

1条回答 默认 最新

  • duanchong3075 2013-12-11 16:55
    关注

    Brackets [] will make an array, so try this and print_r($_POST) and see:

    foreach ($productid as $id ) {
        echo '<input type="text" name="product['.$id.'][s]" />';
        echo '<input type="text" name="product['.$id.'][m]" />';
        echo '<input type="text" name="product['.$id.'][l]" />';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥66 比特币地址如何生成taproot地址
  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决