php 计算 $_POST 中的值

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

Count values in $_POST

php

提问by santa

I have the weirdest problem. I need to count how many values are submitted via $_POST in PHP.
I tried count($_POST['posted'])and I keep on getting 1 instead of 30+ expected values.

我有最奇怪的问题。我需要计算通过 $_POST 在 PHP 中提交的值的数量。
我试过了count($_POST['posted']),我继续得到 1 而不是 30+ 的预期值。

I checked if it is an array and yes it is! Tried; print_r($_POST);and get values

我检查了它是否是一个数组,是的!尝试过;print_r($_POST);并获得价值

Array( ['posted'] => asdasd asdasd ... asdasd )

Array( ['posted'] => asdasd asdasd ... asdasd )

What am I missing?

我错过了什么?



UPDATE:

更新:

$count = count($_POST);
echo $count.' --- ';
print_r($_POST);

Output:

输出:

1 --- Array ( [posted] => asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd )

1 --- 数组([发布] => asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd)

...and

...和

var_dump($_POST);

produces:

产生:

array(1) { ["posted"]=> string(152) "asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd " }

array(1) { ["posted"]=> string(152) "asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd }

and finally...

最后...

var_export($_POST);

delivers:

提供:

array ( 'posted' => 'asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd ', )

数组('发布' => 'asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd asdasd ', )

回答by ceejayoz

$_POSTis always an array, but your $_POST['posted']isn't an array itself.

$_POST总是一个数组,但你$_POST['posted']的不是一个数组本身。

回答by Niklas

To count the number of items in a $_POSTarray, use count($_POST)(not count($_POST['posted']))

要计算$_POST数组中的项目数,请使用count($_POST)(not count($_POST['posted']))

回答by FinalForm

<?php

$my_count = count($_POST);

?>

回答by Dan Lugg

If your form is posting the parameters as a nested array:

如果您的表单将参数作为嵌套数组发布:

<input name="posted[]" />

Then your method should be correct. However based on your example, its a single string of space-separated values, so you'd want to:

那么你的方法应该是正确的。但是,根据您的示例,它是一串空格分隔的值,因此您需要:

$values = explode(' ', $_POST['posted']);
$count = count($values);

回答by darkylmnx

Your array must have been treated earlier but try var_dum($_RESQUEST)it will show you all the posted, url and cookies vars if in the request it's also like it is now it means your form input or any feild names have the same name lol or that you did a treatement

您的数组必须之前已经处理过,但尝试var_dum($_RESQUEST)它会向您显示所有已发布的、url 和 cookie 变量,如果在请求中它也像现在一样,这意味着您的表单输入或任何字段名称具有相同的名称 lol 或者您做了一个治疗

回答by Amit Chaudhari

If your form is posting multiple value for each parameter (posted) then you have to give name for this parameter as array in name attribute:

如果您的表单为每个参数(已发布)发布多个值,那么您必须将此参数的名称作为 name 属性中的数组指定:

<input name="posted[]" value="abc" />
<input name="posted[]" value="opq" />
<input name="posted[]" value="xyz" />

Once you posting this form you will get the count for postedvariable as below :

发布此表单后,您将获得发布变量的计数,如下所示:

echo count($_POST[posted]);

回答by zubaida

$post = $_POST["posted"];        // get posted value
$total = (explode('', $post));   // separate values which have space 
$list = count($total);           // count separated values
echo $list;                      // Print count number