dpnfxk251524 2012-10-02 14:56
浏览 90
已采纳

在php中处理数组

I got this 2 array in a form to be process. However, i only manage to get the output from only one of the array. Sample as below :

<inputs id="location" type="text" name="data[]" value=""/>
<input id="shipval" type="text" name="data[][id]" value=""/>

And in the PHP part is below :

foreach ($_POST ["data"] as $id => $subs) {       

            foreach ($subs as $key=>$sub) {

                $subcategory = $sub;

                if($subs['id']=="$subcategory"){
                    echo $sql = " insert into x(kodLebuhraya,kodSeksyen) values ('".$subs['id']."','".$sub."')";echo "<br>";    
                }else{
                    //echo "hi2";
                    echo $sql = " insert into x(kodLebuhraya,kodSeksyen) values ('".$subs['id']."','".$sub."')";echo "<br>";
                }

            }   

        }

It means one location for one shipval. i have multiple input field for location and shipval. Can you guys enlight me which one is wrong. Thanks in advanced.

  • 写回答

5条回答 默认 最新

  • dtx9763 2012-10-02 15:06
    关注

    So basically you need to pass location and shipval in pairs.

    Try this structure in HTML:

    <label>Set One</label>
    <input class="location" type="text" name="data[location][]" value=""/>
    <input class="shipval" type="text" name="data[shipval][]" value=""/>
    <label>Set Two</label>
    <input class="location" type="text" name="data[location][]" value=""/>
    <input class="shipval" type="text" name="data[shipval][]" value=""/>
    <label>Set Three</label>
    <input class="location" type="text" name="data[location][]" value=""/>
    <input class="shipval" type="text" name="data[shipval][]" value=""/>
    

    And this code for PHP:

      foreach ($_POST['data']['location'] as $key => $location) {       
            $shipVal = $_POST['data']['shipval'][$key];
    
            //now you have a pair of $location and $shipVal
            echo $location.' : '.$shipVal.'<hr>';
    
        }
    

    Avoid using named indexes after unnamed ones ex. <input name="array[][named]" /> you can lose order of fields if one of pair fields is empty.

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

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题