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

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法