PHP 解析 $_POST 数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12021817/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:40:07  来源:igfitidea点击:

PHP Parse $_POST Array?

phparraysparsing

提问by Yinan Wang

A server sends me a $_POST request in the following format:

服务器以以下格式向我发送 $_POST 请求:

POST {
  array1
  {
    info1,
    info2,
    info3
  },
  info4
}

So naturally, I could extract the info# very simply with $_POST['#info']. But how do I get the the three info's in the array1? I tried $_POST['array1']['info1']to no avail.

很自然地,我可以非常简单地使用$_POST['#info']. 但是如何获取array1 中的三个信息?我试过$_POST['array1']['info1']无济于事。

Thanks!

谢谢!

 a:2:  {s:7:"payload";s:59:"{"amount":25,"adjusted_amount":17.0,"uid":"jiajia"}";s:9:"signature";s:40:"53764f33e087e418dbbc1c702499203243f759d4";}

is the serialized version of the POST

是 POST 的序列化版本

回答by Jared Farrish

Use index notation:

使用索引符号:

$_POST['array1'][0]
$_POST['array1'][1]
$_POST['array1'][2] 

If you need to iterate over a variable response:

如果您需要迭代可变响应:

for ($i = 0, $l = count($_POST['array1']); $i < $l; $i++) {
    doStuff($_POST['array1'][$i]);
}

This more or less takes this shape in plain PHP:

这在普通的 PHP 中或多或少采用了这种形式:

$post = array();
$post['info'] = '#';
$post['array1'] = array('info1', 'info2', 'info3');

http://codepad.org/1QZVOaw4

http://codepad.org/1QZVOaw4

So you can see it's really just an array in an array, with numeric indices.

所以你可以看到它实际上只是一个数组中的一个数组,带有数字索引。



Note, if it's an associative array, you need to use foreach():

请注意,如果它是关联数组,则需要使用foreach()

foreach ($_POST['array1'] as $key => $val) {
    doStuff($key, $val);
}

http://codepad.org/WW7U5qmN

http://codepad.org/WW7U5qmN

回答by Anant Dabhi

try

尝试

$_POST['array1'][0]
$_POST['array1'][1]
$_POST['array1'][2]

回答by Bj?rn Thomsen

You can simply use a foreach loop on the $_POST

您可以简单地在 $_POST 上使用 foreach 循环

foreach($_POST["array1"] as $info)
{
    echo $info;
}

or you can access them by their index:

或者您可以通过它们的索引访问它们:

for($i = 0; $i<sizeof($_POST["array1"]); $i++)
{
    echo $_POST["array1"][$i];
}