php 如何打印_r $_POST 数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7093363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print_r $_POST array?
提问by Iladarsda
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
和 test.php 文件
<?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 />
但它只返回这个: <p>0</p><p>Array</p><hr />
What I'm doing wrong?
我做错了什么?
回答by billrichards
The foreach loops work just fine, but you can also simply
foreach 循环工作得很好,但您也可以简单地
print_r($_POST);
Or for pretty printing in a browser:
或者在浏览器中进行漂亮的打印:
echo "<pre>";
print_r($_POST);
echo "</pre>";
回答by Senad Me?kin
<?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.
这会起作用,您的第一个解决方案是尝试打印数组,因为您的值是一个数组。
回答by Brian Driscoll
$_POST is already an array, so you don't need to wrap array() around it.
$_POST 已经是一个数组,所以你不需要用 array() 包裹它。
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.
注意:这是有效的,因为您的id
和value
数组是对称的。如果他们有不同数量的元素,那么你需要采取不同的方法。
回答by Phil
Why are you wrapping the $_POST
array in an array?
为什么要将$_POST
数组包装在数组中?
You can access your "id" and "value" arrays using the following
您可以使用以下方法访问“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) {
// ...
}
回答by Michael Berkowski
You are adding the $_POST
array as the first element to $myarray
. If you wish to reference it, just do:
您将$_POST
数组作为第一个元素添加到$myarray
. 如果您想引用它,只需执行以下操作:
$myarray = $_POST;
However, this is probably not necessary, as you can just call it via $_POST
in your script.
但是,这可能不是必需的,因为您可以通过$_POST
在脚本中调用它。
回答by Headshota
Just:
只是:
foreach ( $_POST as $key => $value) {
echo "<p>".$key."</p>";
echo "<p>".$value."</p>";
echo "<hr />";
}
回答by cwallenpoole
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 );
回答by Gopalakrishna Palem
Came across this 'implode' recently.
最近遇到了这个“内爆”。
May be useful to output arrays. http://in2.php.net/implode
可能对输出数组有用。http://in2.php.net/implode
echo 'Variables: ' . implode( ', ', $_POST);
回答by klennepette
$_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:
$_POST
本身就是一个数组,您不需要从中创建一个数组。您所做的是将$_POST
数组嵌套在新数组中。这就是您打印Array
. 将其更改为:
foreach ($_POST as $key => $value) {
echo "<p>".$key."</p>";
echo "<p>".$value."</p>";
echo "<hr />";
}
回答by mingos
$_POST is already an array. Try this:
$_POST 已经是一个数组。尝试这个:
foreach ($_POST as $key => $value) {
echo "<p>".$key."</p>";
echo "<p>".$value."</p>";
echo "<hr />";
}