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 want to sort multidimensional array values from not null to null. I want
I want to sort array A based on values in Array B actually in
I want to sort a flat, associative array by its values alphabetically and preserve
I want to reverse an array (or any other data structure) but because this
I want to sort the array c. But I don't get the answer a,b,c,d.
I want to sort an array by using uasort() function. I wrote this piece
I want to sort my array: p1q1 p1q10 p1q2 to this order: p1q1 p1q2
I want to sort an array that looks like this (to numerical order instead
I have an array which contains duplicate values. I want to sort the array
I have an array that I want to sort based on the value of

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.