PHP Getting the highest value from an array (from SQL selection)

I have 2 tables: dt_times and dt_reports

dt_times contains a list of times, and dt_reports contains votes (positive / negative) at the corresponding time points.

My code does the following:

  • Picks all the time until today
  • For each of these periods - calculates margin ((positive-negative)/positive)*100
  • The result is stored in an array.

After all iterations, here is var_dump id => margin

 array(8) { [111]=> int(100) [110]=> int(-100) [108]=> int(-100) [100]=> int(100) [97]=> int(100) [92]=> int(100) [59]=> float(-71.4285714286) [58]=> float(-50) } 

I need to select the highest stock, where id is also the highest. Since you can see that 111, 100, 97 and 92 - all have the same value of 100.

My code so far has been as follows:

 while ($row = mysql_fetch_array($result)) { //Get values for quality //calculate margin $arr[$row['id']] = $margin; $arr2[$margin] = $row['id']; } $timeId = $arr2[max($arr)]; 

However, after all cycles, $ timeId is 92, which is the last id in the array, which has a value of 100

I think array_keys might be the answer, but for the love of my life I could not understand them = (

+4
source share
3 answers

Sort the resulting array by key using krsort - http://us.php.net/manual/en/function.krsort.php . It will return the highest key with the highest value as the first element.

  <?php $a = array(10 => 10, 20=>10, 20=>20); krsort($a); var_dump($a); ?> 
+3
source

We start by getting the maximum value of the array http://us.php.net/manual/en/function.max.php , and then just iterate over the array and find the keys with fields corresponding to this maximum value.

 $arr = array(); while ($row = mysql_fetch_array($result)) { //Get values for quality //calculate margin $arr[$row['id']] = $margin; } $max = max($arr); $highestId = 0; foreach($arr as $id => $margin){ if($margin == $max && $id > $highestId){ $highestId = $id; } } 
+1
source

You can do this by creating a suitable array that will be used with usort () a custom comparison function, the code is as follows:

 function cmp($a, $b) { if ($a['id'] == $b['id']) { if ($a['margin'] == $b['margin']) { return 0; } return ($a['margin'] > $b['margin']) ? -1 : 1; } return ($a['id'] > $b['id']) ? -1 : 1; } while ($row = mysql_fetch_array($result)) { //calculate $margin //... $arr[] = array('margin'=>$margin ,'id'=>$row['id']); } usort($arr, 'cmp'); echo 'The highest id with the highest margin is'; echo ' ID: ' . $arr[0]['id'] . ' MARGIN: ' . $arr[0]['margin']; //Print full sorted classification foreach($arr as $val) { echo 'ID:' . $val['id'] . ' MARG: ' . $val['margin'] . '<br>'; } 

This solution will give you $arr , ordered by id, margin from highest to lowest, so the first element of $arr[0] is the highest identifier and margin, and you can loop the rest of the array to print out the full sorted table, and not only the highest element.

+1
source

Source: https://habr.com/ru/post/1438682/


All Articles