douren6874 2014-02-23 22:27
浏览 88
已采纳

爆炸并处理php动态数组

I have a form that is built dynamically. Here's the form input

echo'<input class="input stickyinput" type="number"  name="'.$pestname['scoutlogpestname'].'#'.$obj['card_id'].'" >

The $pestname['scoutlogpestname'] can always be different. The $obj['card_id'] can be any value from 1-50. I placed the # in the name figuring I would need a delimiter to explode with. The array that prints looks like this. Numbers for any values entered and blank for any not entered.

Array
(
    [Aphids#1] => 11
    [Thrips#1] => 5
    [White-Fly#1] => 7
    [Aphids#2] => 
    [Thrips#2] => 1
    [White-Fly#2] => 22
    [Aphids#3] => 4
    [Thrips#3] => 1
    [White-Fly#3] => 
    etc....... possibly to 50
)

Can somebody please give me some insight on how to execute the explode loop so I can process the $pestname['scoutlogpestname'] and the $obj['card_id'] values? Thanks for looking.

  • 写回答

3条回答 默认 最新

  • doutui2016 2014-02-23 22:34
    关注

    Lose that weird hash thing and just use array notation in your form fields. For example...

    <input name="<?= htmlspecialchars($pestname['scoutlogpestname']) ?>[<?= $obj['card_id'] ?>]" ...
    

    This will produce something like

    <input name="Aphids[1]" ...
    <input name="Aphids[2]" ...
    

    When submitted, this will give you an array of arrays in the $_POST super global, eg

    Array
    (
        [Aphids] => Array
            (
                [1] => 11
                [2] => 
            )
    )
    

    You can then iterate each entry and value array, eg

    foreach ($_POST as $pestname => $values) {
        foreach ($values as $card_id => $value) {
            // code goes here
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀