[英]Find highest number in array and work out percentage of the rest
Okay, so i have an array that has this structure. 好的,所以我有一个具有此结构的数组。
[0] => stdClass Object
(
[questionID] => 588
[count] => 2
[answer] => extremely-likely
)
[1] => stdClass Object
(
[questionID] => 588
[count] => 2
[answer] => extremely-unlikely
)
[2] => stdClass Object
(
[questionID] => 588
[count] => 1
[answer] => likely
)
[3] => stdClass Object
(
[questionID] => 588
[count] => 1
[answer] => neither
)
[4] => stdClass Object
(
[questionID] => 588
[count] => 1
[answer] => unsure
)
Okay so basically i first want to find the highest number (count) from this array. 好的,所以基本上我首先要从此数组中找到最高的数字(计数)。
So in the example above it would be 2. 因此在上面的示例中为2。
Now with that highest number saved i want to work out the what percent of 2(the highest number) are the rest of the numbers. 现在保存了该最高数字,我想算出其余数字中2(最高数字)的百分比。
So from my array above, [2] would be 50% of the highest number. 因此,从我上面的数组中,[2]将是最高数字的50%。
Now my efforts so far are shown below: 现在,到目前为止,我的工作如下所示:
private function getFFT($region){
$fft = Question::getFFTCount($this->hw->id);
$numbers = [];
foreach($fft as $answer){
$numbers[] = $answer->count;
}
$findHighest = array_keys($numbers,max($numbers));
return $findHighest;
}
Now when i print_r($findHighest) i get the following. 现在,当我print_r($ findHighest)我得到以下内容。
Array
(
[0] => 0
[1] => 1
)
Does any one know how i can achieve this?. 有谁知道我怎么能做到这一点?
You can save yourself the cost/memory of storing values in the $numbers array if you just keep track of the highest value. 如果只跟踪最高值,则可以节省自己在$ numbers数组中存储值的成本/内存。
private function getFFT(){
$fft = Question::getFFTCount($this->hw->id);
foreach($fft as $answer){
if (empty($findHighest) || $answer->count > $highest) {
$highest = $answer->count;
}
}
return $highest;
}
Then once you have your $highest value, just divide the count by $highest. 然后,一旦您拥有$ highest的价值,只需将计数除以$ highest。 If you need this method to return an array of percentages of each one instead of the value of the highest, you could do this: 如果您需要此方法来返回每个百分比的数组而不是最大值的值,则可以执行以下操作:
private function getFFT(){
$fft = Question::getFFTCount($this->hw->id);
foreach($fft as $answer) {
if (empty($findHighest) || $answer->count > $highest) {
$highest = $answer->count;
}
}
$numnbers = [];
foreach($fft as $answer) {
$numbers[] = $answer->count / $highest;
}
return $numbers;
}
Also, you probably don't need to pass in $region since you are not using it anywhere in the function. 另外,您可能不需要传递$ region,因为您没有在函数中的任何地方使用它。
private function getFFT($region){
$fft = Question::getFFTCount($this->hw->id);
$max=0;
$count=0;
foreach($fft as $answer){
if($answer->count> $max)
{
$max=$answer->count;
$count=1;
}
else if($answer->count== $max)
$count++;
}
$ans = $count/count($fft);
$ans=$ans*100;
return $ans;
}
I don't know if I understood your question correctly, but this is what I came up with: 我不知道我是否正确理解了您的问题,但这是我想到的:
$numbers = [2, 2, 1, 1, 1];
$countValues = array_count_values($numbers);
$highestNumber = max($numbers);
$frequencyOfHighestNumber = $countValues[$highestNumber];
$percentage = 1 / $frequencyOfHighestNumber;
print $percentage;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.