Jump to content

How to retrieve the highest score from a set of array values of a 2D array.


terungwa
Go to solution Solved by Jacques1,

Recommended Posts

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
Share on other sites

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
Share on other sites

  • Solution

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.