如何在PHP中找到数组的模式

如何在PHP中找到数组的模式,php,arrays,mode,Php,Arrays,Mode,我有一个从低到高排序的数组,其中的值超过260k。我已经找到了阵列的平均值和中位数,只需要找出模式 我不能使用PHP拥有的任何数学函数,它必须全部手动完成 我希望它可以只有一个值是模式,但也可以有多个值是模式。我还需要能够记录存储值的次数。例如,数字51出现6次,因此我可以打印这两个值 这是我目前的代码: $amountRecords = 0; $totalValue = 0; $valueArray = array(); // reads in csv file $handle = fope

我有一个从低到高排序的数组,其中的值超过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);
简单

$arr = array(4,6,7,1,4,7,4,7,1);
$freq = array();
for($i=0; $i<count($arr); $i++)
{
   if(isset($freq[$arr[$i]])==false)
   {
       $freq[$arr[$i]] = 1;
   }
   else
   {
       $freq[$arr[$i]]++;
   }
}
$maxs = array_keys($freq, max($freq));

for($i=0; $i<count($maxs); $i++)
{
   echo $maxs[$i] . ' ' . $freq[$maxs[$i]];
   echo '<br />';
}
$arr=数组(4,6,7,1,4,7,4,7,1);
$freq=array();

对于($i=0;$i纯数学解:

    //sample data
$dataArr = ["1", "3", "5", "1", "3", "7", "1", "8", "1"];

//a multidimensional array to hold the keys (extracted fro above) and their values (number of occurrences)
$multiDArr = [];
for ($i = 0; $i < count($dataArr); $i++) {
    $key = $dataArr[$i];

    if (isset($multiDArr[$key])) {
        //key already exists; increment count of its value
        $multiDArr[$key] = $multiDArr[$key] + 1;
    } else {
        //key does nto exist; add it and an inital value of 1
        $multiDArr[$key] = 1;
    }
}

$highestOccuring = 0;
$highestOccuringKey = null;
foreach ($multiDArr as $key => $value) {

    if ($value > $highestOccuring) {
        $highestOccuring = $value;
        $highestOccuringKey = $key;
    }

}

echo "MODE / highest occuring key: " . $highestOccuringKey;
//示例数据
$dataArr=[“1”、“3”、“5”、“1”、“3”、“7”、“1”、“8”、“1”];
//用于保存键(从上面提取)及其值(出现次数)的多维数组
$multiDArr=[];
对于($i=0;$i$value的多数据){
如果($value>$highestOccuring){
$highestOccuring=$value;
$highestOccuringKey=$key;
}
}
回声“模式/最高发生键:”.$highestOccuringKey;

正如我在帖子中所说,它必须手动完成,而不使用PHP拥有的任何数学函数。这包括count、max和min等。请尝试计算模式,然后我们可以帮助您解决。根据上面的代码,看起来您甚至没有尝试过。@WhiteElephant我有一些东西,但它只返回一个值如果有多个,则不是多个。我已使用添加的模式代码编辑了我的原始帖子。此解决方案很好,但如果有多个模式编号,则只选择数组中顺序的第一个。我正在尝试获取最常出现的所有编号,例如{2,2,3,3,1}->我需要得到2和3。你能有其他解决方案吗?很抱歉我的英文多模式解决方案很差:
$values=array\u count\u values($cells);asort($values);$modes=array\u keys($values,max($values));