0

Apologies if this has been asked before but I dont know what the process is called so I dont know how to search for it...

I want to print out the name of the top scorer in an exam. I have built an array that returns the top score:

$tom = 90;
$dick = 80;
$harry = 70;

$n = array($tom, $dick, $harry);
arsort($n);
$keys = array_keys($n);

$top_score_1 = $n[$keys[0]];

echo $top_score_1; //prints 90

However I want it to print the name 'Tom' instead the score. How can I add each persons name to their score so that

echo $top_score_1; //prints Tom

Many thanks for any help,

P

2
  • 1
    You could make a multidimensional array, e.g. $arr = [["score" => 12, "name" = "xy"]...]; Commented Sep 20, 2015 at 20:45
  • 1
    slightly harder to sort multi dimensional arrays if you don't actually need the extra dimension Commented Sep 20, 2015 at 21:05

1 Answer 1

2

i restructured the array - hope that's ok

<?php
$tom = 90;
$dick = 80;
$harry = 70;

$n = array('tom'=>$tom, 'dick'=>$dick, 'harry'=>$harry);
arsort($n);

$first_key = key($n); //key returns the current key, which will just be the first key in this case

echo $first_key; //tom
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.