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 3453576
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:23:25+00:00 2026-05-18T09:23:25+00:00

I want to sort an array in reverse order of values. I used arsort

  • 0

I want to sort an array in reverse order of values. I used arsort php function but the result is not good for me.

Example:

I want to sort next array:

myArray = array("d" => 1, "f" => 2, "b" => 3, "c" => 4,  "e" => 2); 

after using arsort php function the result is:

myArray =array ( "c" => 4, "b" => 3, "e" => 2, "f" => 2, "d" => 1 );

which it is not good because the arsort function doesn’t keep initial order of elements in array.

I want the result like:

myArray =array ( "c" => 4, "b" => 3, "f" => 2, "e" => 2, "d" => 1 );

f before e order, like from original array. keys with same value to not reverse.
Thanks.

  • 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-18T09:23:25+00:00Added an answer on May 18, 2026 at 9:23 am

    Edit:
    This isn’t doable with a built in function and you need to implement your own sorting solution. You can follow this question I opened to understand more.


    It is a coincidence that these solutions work:

    $myArray = array("d" => 1, "f" => 2, "b" => 3, "c" => 4,  "e" => 2);
    uasort($myArray, function($a, $b){
        if($a == $b)
            return 1;
        else
            return $b - $a;
    });
    print_r($myArray);
    

    or

    $myArray = array("d" => 1, "f" => 2, "b" => 3, "c" => 4,  "e" => 2);
    uasort($myArray, function($a, $b){
        return $a <= $b;
    });
    print_r($myArray);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have PHP code that sort an array: arsort($chart); but I do not want
I want to sort associative array by key. I use ksort function but it
I want to sort array A based on values in Array B actually in
I want to sort an array by 'hits', but I also want to look
How can I sort a 2D array in PHP. I want to sort on
I want to sort a flat, associative array by its values alphabetically and preserve
I want to sort the array based on two values that are stored in
I want to sort string array based on on input string , for example
I want to sort my array.... In my array elements are in this order
Using javascript/jquery, I want to sort an array of rgba values to the colours

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.