首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与php的统计比较

与php的统计比较
EN

Stack Overflow用户
提问于 2015-02-20 13:44:48
回答 2查看 32关注 0票数 0

我正在寻找一个人来指出我在正确的方向编码一些统计比较。目前,我查询我的数据库,并将按以下方式获取数据:

主要数据集:

代码语言:javascript
复制
3,4,7,10,5,8,1,3,7

要与之比较的集合可以是这样的,并且可以有多个集合。

代码语言:javascript
复制
4,5,6,9,10,2,3,4,6

现在我需要计算出这两组数据之间的差异--例如,3-4的差值是1,然后我需要选择最大的差异,最一致的,最低的得分。

你会怎么处理这个编码?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-02-20 14:42:00

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

代码语言:javascript
复制
<?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;
}

?>

该代码的输出是:

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2015-02-20 13:49:03

代码语言:javascript
复制
$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;

像这样的东西.并没有真正测试过,但这就是想法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28630427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档