dongpian4954 2017-11-27 10:35
浏览 47
已采纳

如何每次迭代两个数组中的每一个项目,而不是循环遍历PHP中动态html输入的完整循环

I'm having two arrays of fields (id[], names[]) and I want to loop through one item each of the two array per time instead of looping through the complete cycle of id[] before looping through names[] using php

HTML Code

<input type="text" name="id[]" /> &nbsp; &nbsp; <input type="text" name="names[]" /></br>
 <input type="text" name="id[]" /> &nbsp; &nbsp; <input type="text" name="nmaes[]" /></br>
 <input type="text" name="id[]" /> &nbsp; &nbsp; <input type="text" name="names[]" /></br>

PHP Code

<?php
error_reporting( error_reporting() & ~E_NOTICE );
if($_POST['test']=='test')
{
    foreach($_POST['id'] as $key => $value) {
    echo "id $key = $value"."</br>";
}
foreach($_POST['names'] as $key => $value) {
    echo "names $key = $value"."</br>";
}


}

?>

Thanks.

  • 写回答

2条回答 默认 最新

  • douxian6086 2017-11-27 10:40
    关注

    You can count number of element and use it to get your posted data. It's not really nice but that should work.

    Warning, id and name need to be same size!

    if($_POST['test']=='test')
    {
        $count = count($_POST['id']);
        for ($i=0;$i<$count;$i++) {
            $id = $_POST['id'][$i];
            $name = $_POST['name'][$i];
            echo "id $i = $id"."</br>";
            echo "name $i = $name"."</br>";
        }
    }
    

    Other way

    if($_POST['test']=='test')
    {
        foreach ($_POST['id'] as $i => $id) {
            $name = $_POST['name'][$i];
            echo "id $i = $id"."</br>";
            echo "name $i = $name"."</br>";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?