How to print_r $ _POST array?

I have the 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 the test.php file

<?php 

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

?>

But he only returns this: <p>0</p><p>Array</p><hr />

What am I doing wrong?

+5
source share
10 answers
<?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 an array, because your value is an array.

+11
source

Foreach loops work fine, but you can also just

print_r($_POST);

Or for a beautiful print in the browser:

echo "<pre>";
print_r($_POST);
echo "</pre>";
+23
source

$_ POST , () .

:

<?php 

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

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

} 

 ?>

. , id value . , .

+5

:

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

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

} 
+1

$_POST $myarray. , :

$myarray = $_POST;

, , , $_POST script.

+1

$_POST ?

"id" "value",

// 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) {
    // ...
}
+1

"".

May be useful for outputting arrays. http://in2.php.net/implode

echo 'Variables: ' . implode( ', ', $_POST);
+1
source

$_POSTis an array in which you do not need to allocate an array. What you did is nest the array $_POSTin a new array. That is why you are typing Array. Change it to:

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

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

} 
0
source

$ _ POST is already an array. Try the following:

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

Since you have nested arrays, 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 );
0
source

All Articles