?
Solved

easy array question, get highest value or value

Posted on 2008-10-30
3
Medium Priority
?
496 Views
Last Modified: 2008-10-30
I have an array.

$score[7] = 19;
$score[4] = 7;
$score[6]=23;

I want to get the key of the one that has the highest value...
0
Comment
Question by:phenixfilms
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 17

Expert Comment

by:psimation
ID: 22844591
$maxval = "0";
foreach ($score as $key => $val) {
 if ($val > $maxval) {
  $maxkey = $key;
}
}
echo $maxkey;
0
 
LVL 17

Expert Comment

by:psimation
ID: 22844603
OOPS, forgot maxval...

$maxval = "0";
foreach ($score as $key => $val) {
 if ($val > $maxval) {
  $maxval = $val;
  $maxkey = $key;
}
}
echo $maxkey;
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 22844613
$score = array();

$score[7] = 19;
$score[4] = 7;
$score[6]=23;

arsort( $score );
$keys = array_keys( $score );

echo $keys[0];
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question