Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在一个文本字段中计数和读取多个输入_Php_Input_Textfield - Fatal编程技术网

Php 在一个文本字段中计数和读取多个输入

Php 在一个文本字段中计数和读取多个输入,php,input,textfield,Php,Input,Textfield,我需要创建一个文本字段,可以像这样获得多个输入 1,2,3,4 那么输出应该是 输入次数:4 平均数:2.5 问题是我如何计算输入的数量,我的意思是程序如何知道用户在文本字段中键入了多少输入,并使用每个值计算所有输入的总和。有人有什么方法或想法吗 谢谢。使用explode()并分解所有逗号(,)。这将为您提供一个数组 然后,使用count和循环进行计数,得到平均值。还可以使用trim()清除空白。您可以在此处测试代码: explode()-count() $input = "1, 2, 3, 4

我需要创建一个文本字段,可以像这样获得多个输入

1,2,3,4

那么输出应该是

输入次数:4

平均数:2.5

问题是我如何计算输入的数量,我的意思是程序如何知道用户在文本字段中键入了多少输入,并使用每个值计算所有输入的总和。有人有什么方法或想法吗

谢谢。

使用
explode()并分解所有逗号(,)。这将为您提供一个数组


然后,使用count和循环进行计数,得到平均值。还可以使用
trim()
清除空白。

您可以在此处测试代码:

explode()
-
count()
$input = "1, 2, 3, 4";

//remove all whitespace
$input = str_replace(' ', '', $input); 

//turn the string into an array of integers
$numbers= array_map('intval', explode(',', $input)); 

//the number of elements
$count = count( $numbers );

//sum the numbers
$sum = array_sum( $numbers );

//print the number of inputs, a new line character, and the mean
echo "Number of inputs: $count\r\n"; 
echo 'Mean: ' . $sum / $count;