+ Reply to Thread
Results 1 to 3 of 3

Thread: Returning Value from Max(Key) in Array

  1. #1
    learning_brain is offline x10 Sophmore learning_brain is an unknown quantity at this point
    Join Date
    Apr 2010
    Location
    UK, Midlands
    Posts
    170

    Returning Value from Max(Key) in Array

    Interesting one this and I have searched high and low for an answer...

    I have an associative array where image types are given a score.

    The array is set up in a loop so that the score is used as the key and the value is the name of the image type.

    PHP Code:
    $imageScores [$score] = $imageType
    I am trying to return the image type (i.e. the value) from that array where it has the highest key (score).

    I can't use a simple max() because it will only return a max value and not a max key.

    Am I doing this the wrong way round? Should I set up ...

    PHP Code:
    $imageScores[$imageType] = $score
    .. I can then easily obtain the highest score, but how do I get the associated key image type?

    It's probably simple but I can't get my head round it.

    Any help would be appreciated.

    Richard
    Last edited by learning_brain; 11-04-2010 at 05:55 PM.

  2. #2
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: Returning Value from Max(Key) in Array

    Try using krsort, it'll sort the array descending by the key rather than the value:
    Code:
    //Setup your array:
    $imageScores[$score] = $imageType;
    //etc
    
    //Sort by key, max score is now the first item in the array
    krsort($imageScores, SORT_NUMERIC);
    
    //Get the first item in the array, there are hundreds of ways you could do this and this is just one:
    $maxValue = reset($imageScores);
    If you need the key (score) as well as the value (as I suspect you might), the reset() method will not work and you may have to do something like a foreach or extract the key values:
    Code:
    //Setup your array:
    $imageScores[$score] = $imageType;
    //etc
    
    //Get the scores as an array
    $scoresArray = array_keys($imageScores);
    
    //Get the max value
    $maxKey = max($scoresArray);
    $maxValue = $imageScores[$maxKey];
    There are probably hundreds of other ways of doing it too.
    Last edited by lemon-tree; 11-04-2010 at 06:27 PM.

  3. #3
    learning_brain is offline x10 Sophmore learning_brain is an unknown quantity at this point
    Join Date
    Apr 2010
    Location
    UK, Midlands
    Posts
    170

    Re: Returning Value from Max(Key) in Array

    This is perfect - the 2nd method is ideal and can't believe I couldn't get my head round that.

    I've also changed the scoring to percentage which works even better!

    Thanks lemon-tree...yet again.

+ Reply to Thread

Similar Threads

  1. Returning javascript with php
    By thenewprogrammer in forum Programming Help
    Replies: 5
    Last Post: 12-06-2009, 05:33 PM
  2. Returning to x10 - Few Questions
    By mcklovin in forum Free Hosting
    Replies: 7
    Last Post: 10-04-2009, 01:03 PM
  3. SSI returning garbage
    By rodrigu5 in forum Free Hosting
    Replies: 1
    Last Post: 06-18-2009, 02:39 PM
  4. x10 Rewards Returning Soon
    By Corey in forum News and Announcements
    Replies: 82
    Last Post: 01-02-2009, 09:21 AM
  5. Returning member
    By dWhite in forum Introductions
    Replies: 13
    Last Post: 11-28-2008, 03:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers