Solved

easy array question, get highest value or value

Posted on 2008-10-30
3
491 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
  • 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 125 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

827 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