0

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
3
  • substr($str, 0, -1)? Commented Apr 5, 2013 at 20:43
  • Looking at your example I don't think that this works as you describe. It should be something like echo $text1[$p]; instead of echo print_r($p[$i]); Commented Apr 5, 2013 at 21:01
  • thanks, it worked without print_r() Commented Apr 6, 2013 at 9:23

2 Answers 2

2
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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.