猿问

显示$ _POST的内容一一

您好,我正在使用POST方法提交表单,我希望其内容与上一个表单一一回显。到目前为止,我正在使用


<?php foreach($_POST as $data){

        echo $data;

?>

它显示了$ _POST的整个数组,如何使用通用的“ for”循环使其不回显数组的最后一项呢?它似乎不起作用


    <?php 

    $length=count($_POST)-1;

    for($i=0; $i<$length; $i++) {

        echo $_POST[$i];

        ?>

        <br>

    <?php } ?>

我收到5个错误,存在回显线的未定义偏移量0到4


四季花海
浏览 166回答 2
2回答

宝慕林4294392

请执行下列操作:<?php&nbsp; &nbsp; $counter = 0;&nbsp; &nbsp; $lastItemOrder = count($_POST);&nbsp; &nbsp; foreach($_POST as $value) {&nbsp; &nbsp; &nbsp; &nbsp; $counter++;&nbsp; &nbsp; &nbsp; &nbsp; if( $counter !== $lastItemOrder) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $value;&nbsp; &nbsp; &nbsp; &nbsp; }?>&nbsp; &nbsp; &nbsp; &nbsp; <br><?php&nbsp;&nbsp; &nbsp; } ?>您的循环仅获得数字索引,例如$ _POSR [0],$ _ POST [1] ...如果在HTML中输入元素的属性名称也为数字,例如name =“ 0”等,则这将起作用。foreach在数组上独立于索引,数字或字符串执行循环。试试这个:<?php&nbsp; &nbsp; $counter = 0;&nbsp; &nbsp; $lastItemOrder = count($_POST);&nbsp; &nbsp; foreach($_POST as $index => $value) {&nbsp; &nbsp; &nbsp; &nbsp; $counter++;&nbsp; &nbsp; &nbsp; &nbsp; if( $counter !== $lastItemOrder) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $index . ": " . $value;&nbsp; &nbsp; &nbsp; &nbsp; }?>&nbsp; &nbsp; &nbsp; &nbsp; <br><?php&nbsp;&nbsp; &nbsp; } ?>

翻过高山走不出你

好的,现在我明白了,我不知道关联数组和数字数组之间的区别。我用if语句修复了它
随时随地看视频慕课网APP
我要回答