SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    USA
    Posts
    633
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Max value in an array()

    Hi Guys

    Please help me

    Suppose I have an array()

    Code:
    $colours = array('blue' => 15, 'red' => 21, 'green' => 55);
    I want to find out the $colour with the highest value.

    Is there a way to do this please without looping through the array()

    Thanks
    DD

  2. #2
    SitePoint Guru r2d2's Avatar
    Join Date
    Dec 2003
    Location
    In my van, fool!
    Posts
    646
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should do the trick:

    PHP Code:
    $maxs array_keys($coloursmax($colours));
    $colour $maxs[0]; 
    We get the array keys of the max value (which returns an array itself), then take the first value.

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    or you could just asort the array.

  4. #4
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    USA
    Posts
    633
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r2d2 View Post
    This should do the trick:

    PHP Code:
    $maxs array_keys($coloursmax($colours));
    $colour $maxs[0]; 
    We get the array keys of the max value (which returns an array itself), then take the first value.
    Thank you so much, it worked for me

    DD


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •