Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7037157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:30:37+00:00 2026-05-28T01:30:37+00:00

I tried to get the 2nd highest value from an array and I simply

  • 0

I tried to get the 2nd highest value from an array and I simply want to know if it is possible to do something like MAX -1 OR I absolutely need to sort by table and the 2nd highest value

private function max_key($array) {
    foreach ($array as $key => $val) {
    if ($val == max($array))  return $key;
    }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:30:38+00:00Added an answer on May 28, 2026 at 1:30 am
     $a = Array ( 1, 2, 40 , 100);
     echo max($a) - 1; // definitely not 40
    

    So yes, you’d need to rsort() (built-in PHP function, sorts the array with highest values first), and then take the 2nd value from the list.

    Note that e.g. array( 1 , 10 , 5 , 10 ) has second value == first value; if you want the second-largest unique value, run it through array_unique() first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried many things but I always get cannot convert string to membershipuser from
I tried to make an independent copy of an array but couldnt get one.
I have something like this : first level 1 2nd level 1 3rd level
I tried to get avatar at single (post) page by these codes <?php echo
has anyone ever tried to get the list of all the movieclips (even the
UPDATE As I've tried to get MEF working throughout my application, I'm coming across
How to allow TextBlock 's text to be selectable? I tried to get it
I'm coding some c# against Active Directory and have tried endlessly to get this
I tried the following code to get an alert upon closing a browser window:
I tried running 'diff' against two source directories get a patch file with a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.