Solved

easy array question, get highest value or value

Posted on 2008-10-30
3
492 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Returning errors to display in a dialog box - Ajax, Json, Php 16 50
Reference key in foreach loop 4 38
Multi line FPDF footer: 3 24
How to use session variables in php? 22 46
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

756 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