2015-02-09 55 views
0

我需要创建一个文本字段,可以得到这样的多输入...计数和一个文本字段读取多个输入

1,2,3,4

然后输出应该是...

编号输入的:4

平均数:2.5

问题是如何计算输入的数量,我的意思是程序如何知道用户输入到文本字段中的输入数量,并使用每个值来计算所有输入的总和。有人有任何方法或任何想法?

谢谢。

+3

'爆炸()' - '计数()' – 2015-02-09 16:51:17

回答

0

使用explode();并将所有逗号(,)分解。这会给你一个数组。

从那里,使用计数和循环进行计数并得到平均值。使用trim() aswel来摆脱空的空间。

0

您可以测试这里的代码:http://writecodeonline.com/php/

$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; 
相关问题