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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?