php - Find highest number in array and work out percentage of the rest -


okay, have array has 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] =>     )  [3] => stdclass object     (         [questionid] => 588         [count] => 1         [answer] => neither     )  [4] => stdclass object     (         [questionid] => 588         [count] => 1         [answer] => unsure     ) 

okay first want find highest number (count) array.

so in example above 2.

now highest number saved want work out percent of 2(the highest number) rest of numbers.

so array above, [2] 50% of highest number.

now efforts far shown below:

private function getfft($region){     $fft = question::getfftcount($this->hw->id);      $numbers = [];     foreach($fft $answer){         $numbers[] = $answer->count;     }      $findhighest = array_keys($numbers,max($numbers));      return $findhighest; } 

now when print_r($findhighest) following.

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

does 1 know how can achieve this?.

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

private function getfft(){     $fft = question::getfftcount($this->hw->id);      foreach($fft $answer){         if (empty($findhighest) || $answer->count > $highest) {             $highest = $answer->count;         }     }      return $highest; } 

then once have $highest value, divide count $highest. if need method return array of percentages of each 1 instead of value of highest, this:

private function getfft(){     $fft = question::getfftcount($this->hw->id);      foreach($fft $answer) {         if (empty($findhighest) || $answer->count > $highest) {             $highest = $answer->count;         }     }      $numnbers = [];     foreach($fft $answer) {         $numbers[] = $answer->count / $highest;     }       return $numbers; } 

also, don't need pass in $region since not using anywhere in function.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -