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

报告相同问题?