terungwa Posted June 13, 2014 Share Posted June 13, 2014 I have mysql select query shown below retrieving data from a database: $sql = " SELECT student, sum( test_score ) as score FROM test INNER JOIN level ON test.level_id = level.level_id GROUP BY student" $result = $mysqli->query($sql, MYSQLI_STORE_RESULT); if($result) { while ($row = $result->fetch_array(MYSQLI_ASSOC)) { var_dump($row); } } On inspection of the retrieved array with var_dump(), i have these result below: array (size=2) 'John' => string 'English' (length=2) 'Score' => string '20' (length=3) array (size=2) 'Mary' => string 'Math' (length=3) 'Score' => string '35' (length=3) array (size=2) 'Samuel' => string 'Physics' (length=3) 'Score' => string '5' (length=3) How do I get at and print out the highest score from this set of array values within this context. I tried this echo max($row['count']) but i understandably got this feedback:Warning: max(): When only one parameter is given, it must be an array Thanks. Link to comment https://forums.phpfreaks.com/topic/289147-how-to-retrieve-the-highest-score-from-a-set-of-array-values-of-a-2d-array/ Share on other sites More sharing options...
mac_gyver Posted June 13, 2014 Share Posted June 13, 2014 add ORDER BY score DESC to the query to get the rows in the result set ordered from highest to lowest score and if you only want the one row with the highest score, add LIMIT 1 to the end of that on the query. Link to comment https://forums.phpfreaks.com/topic/289147-how-to-retrieve-the-highest-score-from-a-set-of-array-values-of-a-2d-array/#findComment-1482632 Share on other sites More sharing options...
terungwa Posted June 13, 2014 Author Share Posted June 13, 2014 add ORDER BY score DESC to the query to get the rows in the result set ordered from highest to lowest score and if you only want the one row with the highest score, add LIMIT 1 to the end of that on the query. Thanks mac_gyver for your response, but that is not what i need. I am retrieving all elements from the result set in a table. What I need in addition to the table is to write out a small summary stating that the student with the highest score is ......................................... So my task is to get at and extract the highest array value for this purpose. Link to comment https://forums.phpfreaks.com/topic/289147-how-to-retrieve-the-highest-score-from-a-set-of-array-values-of-a-2d-array/#findComment-1482634 Share on other sites More sharing options...
mac_gyver Posted June 13, 2014 Share Posted June 13, 2014 moving thread to the php help forum section as that's not a mysql question. Link to comment https://forums.phpfreaks.com/topic/289147-how-to-retrieve-the-highest-score-from-a-set-of-array-values-of-a-2d-array/#findComment-1482635 Share on other sites More sharing options...
mac_gyver Posted June 13, 2014 Share Posted June 13, 2014 assuming that the data in the array isn't already sorted as needed, the most general purpose way of ordering/finding values in a multiple dimensional array is to use usort() and write a function that orders the data the way you want. Link to comment https://forums.phpfreaks.com/topic/289147-how-to-retrieve-the-highest-score-from-a-set-of-array-values-of-a-2d-array/#findComment-1482636 Share on other sites More sharing options...
Jacques1 Posted June 13, 2014 Share Posted June 13, 2014 Since terungwa just wants the highest score, there's no need for sorting the array. A simple loop is enough: <?php $student_scores = array( array( 'student' => 'John', 'score' => '20' ), array( 'student' => 'Mary', 'score' => '35' ), array( 'student' => 'Samuel', 'score' => '5' ), ); $max_score = null; foreach ($student_scores as $student_score) { if (is_null($max_score) || $student_score['score'] > $max_score) { $max_score = $student_score['score']; } } If the summary comes after the table, you can even do this in the main loop. If you want “the” student with the highest score, you first need to define an additional unique criterion in case multiple students have the same score. Link to comment https://forums.phpfreaks.com/topic/289147-how-to-retrieve-the-highest-score-from-a-set-of-array-values-of-a-2d-array/#findComment-1482637 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.