11

I have following table.

<form method="post" action="test.php">
  <input name="id[]" type="text" value="ID1" />
  <input name="value[]" type="text" value="Value1" />
  <hr />

  <input name="id[]" type="text" value="ID2" />
  <input name="value[]" type="text" value="Value2" />
  <hr />

  <input name="id[]" type="text" value="ID3" />
  <input name="value[]" type="text" value="Value3" />
  <hr />

  <input name="id[]" type="text" value="ID4" />
  <input name="value[]" type="text" value="Value4" />
  <hr />

  <input type="submit" />
</form>

And test.php file

<?php 

  $myarray = array( $_POST);
  foreach ($myarray as $key => $value)
  {
    echo "<p>".$key."</p>";
    echo "<p>".$value."</p>";
    echo "<hr />";
  }

?>

But it is only returning this: <p>0</p><p>Array</p><hr />

What I'm doing wrong?

jww
  • 97,681
  • 90
  • 411
  • 885
Iladarsda
  • 10,640
  • 39
  • 106
  • 170
  • Is there a reason why you are trying to collect multiple textfields into the same name? Also if you aren't concerned about how pretty it looks, `echo "

    " . print_r($value, TRUE) . "

    ";` could be your friend.
    – KyleWpppd Aug 17 '11 at 13:41
  • @Kyle - in the end this will be a BIG table of the inputes to update the DB. – Iladarsda Aug 17 '11 at 13:46
  • How are you filling out the table? Wouldn't a `*.sql` file be a decent choice assuming the text exists somewhere already? – KyleWpppd Aug 17 '11 at 13:54
  • @Kyle - for me that would be the easiest - but for someone non technical - file where we are droping all of the content of the table form DB, fill / change the inputs and submit update is the correct way of doing it. – Iladarsda Aug 17 '11 at 14:56
  • Possible duplicate of [$\_POST print variable name along with value](http://stackoverflow.com/questions/3489387/php-post-print-variable-name-along-with-value) – jww May 28 '16 at 18:10

11 Answers11

32

The foreach loops work just fine, but you can also simply

print_r($_POST);

Or for pretty printing in a browser:

echo "<pre>";
print_r($_POST);
echo "</pre>";
billrichards
  • 2,041
  • 4
  • 25
  • 35
16
<?php 

 foreach ($_POST as $key => $value) {
  echo '<p>'.$key.'</p>';
  foreach($value as $k => $v)
  {
  echo '<p>'.$k.'</p>';
  echo '<p>'.$v.'</p>';
  echo '<hr />';
  }

} 

 ?>

this will work, your first solution is trying to print array, because your value is an array.

mario
  • 144,265
  • 20
  • 237
  • 291
Senad Meškin
  • 13,597
  • 4
  • 37
  • 55
5

$_POST is already an array, so you don't need to wrap array() around it.

Try this instead:

<?php 

 for ($i=0;$i<count($_POST['id']);$i++) {

  echo "<p>".$_POST['id'][$i]."</p>";
  echo "<p>".$_POST['value'][$i]."</p>";
  echo "<hr />";

} 

 ?>

NOTE: This works because your id and value arrays are symmetrical. If they had different numbers of elements then you'd need to take a different approach.

Brian Driscoll
  • 19,373
  • 3
  • 46
  • 65
2

You are adding the $_POST array as the first element to $myarray. If you wish to reference it, just do:

$myarray = $_POST;

However, this is probably not necessary, as you can just call it via $_POST in your script.

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
2

Why are you wrapping the $_POST array in an array?

You can access your "id" and "value" arrays using the following

// assuming the appropriate isset() checks for $_POST['id'] and $_POST['value']

$ids = $_POST['id'];
$values = $_POST['value'];

foreach ($ids as $idx => $id) {
    // ...
}

foreach ($values as $idx => $value) {
    // ...
}
Phil
  • 157,677
  • 23
  • 242
  • 245
1

Came across this 'implode' recently.

May be useful to output arrays. http://in2.php.net/implode

echo 'Variables: ' . implode( ', ', $_POST);
Paul
  • 26,170
  • 12
  • 85
  • 119
Gopalakrishna Palem
  • 1,705
  • 1
  • 20
  • 34
1

Just:

foreach ( $_POST as $key => $value) {

  echo "<p>".$key."</p>";
  echo "<p>".$value."</p>";
  echo "<hr />";

} 
Headshota
  • 21,021
  • 11
  • 61
  • 82
1

Because you have nested arrays, then I actually recommend a recursive approach:

function recurse_into_array( $in, $tabs = "" )
{
    foreach( $in as $key => $item )
    {
        echo $tabs . $key . ' => ';
        if( is_array( $item ) )
        {
            recurse_into_array( $item, $tabs . "\t" );
        }
        else
        {
            echo $tabs . "\t" . $key;
        }
    }
}

recurse_into_array( $_POST );
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
0

As you need to see the result for testing purpose. The simple and elegant solution is the below code.

echo "<pre>";
print_r($_POST);
echo "</pre>";
Ahmad Sharif
  • 4,141
  • 5
  • 37
  • 49
0

$_POST is an array in itsself you don't need to make an array out of it. What you did is nest the $_POST array inside a new array. This is why you print Array. Change it to:

foreach ($_POST as $key => $value) {

  echo "<p>".$key."</p>";
  echo "<p>".$value."</p>";
  echo "<hr />";

} 
klennepette
  • 3,176
  • 22
  • 23
0

$_POST is already an array. Try this:

foreach ($_POST as $key => $value) {
    echo "<p>".$key."</p>";
    echo "<p>".$value."</p>";
    echo "<hr />";
} 
mingos
  • 23,778
  • 12
  • 70
  • 107