与php进行统计比较

时间:2015-02-20 13:44:49

标签: php mysql comparison

我正在找人指出正确的方向来编写一些统计比较。目前我查询我的数据库,并将获得如下数据:

主要数据集:

3,4,7,10,5,8,1,3,7

设置比较可能是这样的,并且可以有多个集合。

4,5,6,9,10,2,3,4,6

现在我需要弄清楚这两组数据之间的区别 - 例如,3-4之间的差异是1.然后我需要选择最大的差异,最大的同意和最低的得分。

你会如何解决这个问题呢?

2 个答案:

答案 0 :(得分:0)

$max_diff = 0;
for ($i = 0; $i < (min(count($array1), count($array2));$i++){
 if (($array1[$i]-$array2[$i]) > $max_diff ) $max_diff = $array1[$i]-$array2[$i];
}

echo $max_diff;

类似的东西......实际上没有测试过,但这就是想法。

答案 1 :(得分:0)

我建议使用函数array_walk(),将第一个数组作为第一个参数传递,将第二个数组作为第三个可选参数传递。它看起来像这样:

<?php

$array_1 = array(3,4,7,10,5,8,1,3,7);
$array_2 = array(4,5,6,9,10,2,3,4,6);
    // making a copy, because the callback function
    // works on the actual value
$array_1_copy = $array_1;

echo '<pre>';
array_walk($array_1_copy, 'difference', $array_2);
echo "\nThe maximum difference is: ". max($array_1_copy);
echo "\nThe minimum difference is: ". min($array_1_copy);
echo '</pre>';

    // the callback function, takes the 1st param as reference
function difference(&$value_1, $index_1, $array_2) {
    $difference = abs($value_1 - $array_2[$index_1]);
    echo "Difference between $value_1 and {$array_2[$index_1]} is $difference\n";
    $value_1 = $difference;
}

?>

此代码的输出为:

Difference between 3 and 4 is 1
Difference between 4 and 5 is 1
Difference between 7 and 6 is 1
Difference between 10 and 9 is 1
Difference between 5 and 10 is 5
Difference between 8 and 2 is 6
Difference between 1 and 3 is 2
Difference between 3 and 4 is 1
Difference between 7 and 6 is 1

The maximum difference is: 6
The minimum difference is: 1