php - 如何在 PHP 中查找数组的众数

标签 php arrays mode

我有一个从低到高排序的数组,其中有超过 260k 个值。我已经找到了数组的平均值和中位数,只需要找出众数?

我无法使用 PHP 拥有的任何数学函数,它必须全部手动完成。

我希望这样可以只有一个值作为模式,但也可以有多个值作为模式。我还需要能够记录该值的存储次数。例如,数字 51 出现了 6 次,因此我可以打印这两个值。

这是我到目前为止的代码:

$amountRecords = 0;
$totalValue = 0;
$valueArray = array();

// reads in csv file
$handle = fopen('Task1-DataForMeanMedianMode.csv', 'r');
// to skip the header names/values
fgetcsv($handle);

// creates array containing variables of csv file in ascending order
while(($row = fgetcsv($handle, "\r")) != FALSE)
{

    // if amountRecords equals 0
    if($amountRecords == 0)
    {

        // adds value from csv to array
        $valueArray[] = $row[1];

    } // else amountRecords does not equal 0
    else 
    {

        // if the value in array location before is greater than the current value from the csv file
        if($valueArray[$amountRecords - 1] > $row[1])
        {

             // the current array location becomes the one in the location before
             $valueArray[] = $valueArray[$amountRecords - 1];
             // add the value from the csv to the location before
             $valueArray[$amountRecords - 1] = $row[1];

         } // if the value in the location before is not greater than the current value in the csv file
         else 
         {

             // adds value from csv to array
             $valueArray[] = $row[1];

         }

    }

    // calculates the total value of the values in csv file
    $totalValue = $totalValue + $row[1];
    // calculates the number of values in the csv file
    $amountRecords++;

}    

// calculate average value of payments
$averageValue = $totalValue / $amountRecords;
// limit integer to 2 decimal place
$average = number_format($averageValue,2,'.','');

// finds middle value
$middle = floor(($amountRecords / 2) - 1);

// calculates the median value
// if array size is even
if($amountRecords % 2 == 0)
{

    // calculates median
    $median = $valueArray[$middle];

} 
else // if array size is odd
{

    // calculate low and high values
    $low = $valueArray[$middle];
    $high = $valueArray[$middle + 1];
    // calculates median
    $median = (($low + $high) / 2);

}

// works out mode
// creates array count
$count = array();
// for each value in the valueArray
foreach( $valueArray as $value )
{

    if( isset( $count[$value] ))
    {

        $count[$value]++;

    }
    else
    {

        $count[$value] = 1;

    }

}

$mostCommon = "";
$iter = 0;

foreach( $count as $k => $v )
{

     if( $v > $iter )
     {

         $mostCommon = $k;
         $iter = $v;

     }

}

$modeArray = array( "mode" => $mostCommon , "count" => $iter );

最佳答案

数字集的众数是出现次数最多的数字。您可以使用类似于以下代码的 PHP 来完成此操作:

$values = array_count_values($valueArray); 
$mode = array_search(max($values), $values);

关于php - 如何在 PHP 中查找数组的众数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12036114/

相关文章:

PHP in_array 行为让我感到惊讶

php - 幸运抽奖概念 : SQL Query to get random option based on percentage of appearance

php - 如何在 Laravel 中配置并行测试数据库?

fetch_array 中的 PHP MySQL 数组

c - 读取以零开头的 int 并将其存储到 C 中的数组中

php - 使用 Codeigniter 更新 MySql 中的数据不起作用

c++ - 让用户创建自己的类实例

hadoop - Hive始终以本地模式运行

java - 如何从 int 类型的方法返回多个整数

python - 在不对结果进行排序的情况下计算数据帧的模式