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

  • SEARCH
  • Home
  • 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 9196843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:56:22+00:00 2026-06-17T21:56:22+00:00

ksort ($votes); foreach ($votes as $total => $contestant){ $ordervotes[]= $contestant; } echo <li> And

  • 0
ksort ($votes);
        foreach ($votes as $total => $contestant){
        $ordervotes[]= $contestant;
        } 

        echo "<li> And the winner is: {$ordervotes[4]}</li>";
        echo "<li> And the loser is: {$ordervotes[0]}</li>";
        echo "<li> {$ordervotes[1]} came second last</li>";

This works fine when none of the ‘$total’s are the same, if they are the same i get an error code. I realise I could use the ‘max/min’ to get the first and last elements of the array, but how do i go about finding the second last?

Thank you

Joe

  • 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-06-17T21:56:23+00:00Added an answer on June 17, 2026 at 9:56 pm

    Why don’t you try:

    echo $votes[count($votes)-2];
    

    You also don’t need to populate another array with the same values – you can keep them in $votes. You might also want to look into sorting your array by value instead of by key (which I assume you’re trying to do).


    If you’re expecting duplicate keys, you need to remodel the way you’re storing your data. Consider using a multidimensional array:

    $votes = array(
       array('name'=>'John','vote'=>10),
       array('name'=>'James','vote'=>11),
       array('name'=>'Jimmy','vote'=>13),
    );
    

    You will be able to sort this array using this function and code:

    // This function will sort your array
    function aasort (&$array, $key) {
        $sorter=array();
        $ret=array();
        reset($array);
        foreach ($array as $ii => $va) {
            $sorter[$ii]=$va[$key];
        }
        asort($sorter);
        foreach ($sorter as $ii => $va) {
            $ret[$ii]=$array[$ii];
        }
        $array=$ret;
    }
    
    // Sort the array by the 'vote' key
    aasort($votes,"vote");
    
    // Echo out the name of the second-last person
    echo $votes[count($votes)-2]['name'];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use PHP's ksort to sort this array: Array( [district_name] => District
Ok, I need keys to be preserved within this array and I just want
I am using Access and have this SQL SELECT land.id, land.official_name, vaksiner.vaksiner FROM land
I have a array like this: $array = array( [1]=>'something', [0.2]=>'something', [0.1]=>'something', [0.3]=>'something', [0.10]=>'something'
I have a 2D array that looks like this: array(2) { [45]=> array(5) {
im getting this error: Parse error: syntax error, unexpected T_ECHO in /home/labvc/public_html/AT/site/getimages.php on line
I am using which code which returns taxonomies which match 2 values. Everything works
So I have this custom object i made called 'Box', it's throwing the error
On our last project we ended up with a shared test fixture for our
I know that ksort() is used to sort array by keys . The max

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.