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 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理