douquqiang1513 2015-06-25 11:39
浏览 60
已采纳

找到数组中的最大数字,并计算出其余部分的百分比

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.

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.

So from my array above, [2] would be 50% of the highest number.

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.

Array
(
    [0] => 0
    [1] => 1
) 

Does any one know how i can achieve this?.

  • 写回答

3条回答 默认 最新

  • doulao1966 2015-06-25 12:02
    关注

    You can save yourself the cost/memory of storing values in the $numbers array if you just keep track of the highest value.

    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. 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?