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