简体   繁体   中英

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.

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

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM