从php中的多维数组中删除最后一个元素

从php中的多维数组中删除最后一个元素

问题描述:

I have a textbox that iterates 5 times and displays values from the textbox into an array.

<form method="post" action="test.php">   
 <?php for($i = 0; $i < 5; $i++) { 
     echo "<input type='text' name='text1[]'/>";    
  } ?>
<input type="submit" name="confirm" value="confirm" />
</form>


<?php
  $text1 = $_POST['text1'];
  $count= count($text1);
  if(isset($_POST['confirm'])) { 
    for($p = 0; $p < $count; $p++) {
       echo print_r($p[$i]);
    }
  }
?>

I want to remove the last value (which is that repeating number 1) from the data and only display the names. The output of above is as follows:-

John1
Jack1
Peter1
Jane1
Jill1

我有一个迭代5次的文本框,并将文本框中的值显示为数组。 p> &lt; form method =“post”action =“test.php”&gt; &lt;?php for($ i = 0; $ i&lt; 5; $ i ++){ echo“&lt; input type ='text'name ='text1 []'/&gt;”; }?&gt; &lt; input type =“submit”name =“confirm”value =“confirm”/&gt; &lt; / form&gt; &lt;?php $ text1 = $ _POST ['text1']; $ count = count($ text1); if(isset($ _ POST ['confirm'])){ for($ p = 0; $ p&lt; $ count; $ p ++){ echo print_r($ p [$ i]); } } ?&gt; code> pre>

我想删除最后一个 数据中的值(重复数字1),仅显示名称。 上面的输出如下: - p>

  John1 
Jack1 
Peter1 
Jane1 
Jill1 
  code>  pre> 
  div>

echo print_r($p[$i]);

print_r prints content of $p[$i] and returns 1 that is passed to echo (and printed next to desired output). You don't need print_r here.

print_r sends $p[$i] to the output buffer and then it returns a boolean result, which will be true (or 1 when echoed out).

So, the solution is simply to not use print_r.

Always read the documentation when you are unsure about something. Pretty much anything you want to know about PHP can be found there.