[php] How to print_r $_POST array?

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?

This question is related to php variables post

The answer is


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>";

Came across this 'implode' recently.

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

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

Just:

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

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

} 

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

$_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.


$_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 />";

} 

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 );

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>";

$_POST is already an array. Try this:

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

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.


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to variables

When to create variables (memory management) How to print a Groovy variable in Jenkins? What does ${} (dollar sign and curly braces) mean in a string in Javascript? How to access global variables How to initialize a variable of date type in java? How to define a variable in a Dockerfile? Why does foo = filter(...) return a <filter object>, not a list? How can I pass variable to ansible playbook in the command line? How do I use this JavaScript variable in HTML? Static vs class functions/variables in Swift classes?

Examples related to post

How to post query parameters with Axios? How can I add raw data body to an axios request? HTTP POST with Json on Body - Flutter/Dart How do I POST XML data to a webservice with Postman? How to set header and options in axios? Redirecting to a page after submitting form in HTML How to post raw body data with curl? How do I make a https post in Node Js without any third party module? How to convert an object to JSON correctly in Angular 2 with TypeScript Postman: How to make multiple requests at the same time