【问题标题】:statistical comparisons with php与php的统计比较
【发布时间】:2015-02-20 13:44:48
【问题描述】:

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

主要数据集:

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

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

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

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

你会如何处理这个编码?

【问题讨论】:

    标签: php mysql comparison


    【解决方案1】:

    我建议使用函数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
    

    【讨论】:

      【解决方案2】:
      $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;
      

      类似的东西......实际上并没有测试它,但就是这样。

      【讨论】:

      • 你应该先分解这些集合。
      • 是的,如果您没有数组,但逗号分隔的值首先由逗号爆炸。
      猜你喜欢
      • 2011-02-10
      • 2014-01-30
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多