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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!