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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:32:29+00:00 2026-06-05T23:32:29+00:00

Consider the following associative array $arr = Array ( [banana] => 2 [cherry] =>

  • 0

Consider the following associative array

$arr = Array
(        
    [banana] => 2
    [cherry] => 1
    [orange] => 3
    [grapefruit] => 1
    [apple] => 1
)

I want to sort it in a way that would be similar to the PLSQL term: A DESC, B ASC
(where A is the value and B is the key) meaning:

$arr = Array
(
    [orange] => 3
    [banana] => 2
    [apple] => 1
    [cherry] => 1
    [grapefruit] => 1        
)

so that orange and banana are first because of the VALUE, but then I have apple, cherry and grapefruit in alphabetical order because they have the same VALUE.

What I tried:

1. to run ksort() and then asort()/rsort() hoping that the second sort will bump up orange and banana to the beginning of the array without messing up the alphabetical sort of the other 3 items. I was wrong. it does messes everything up. So I checked out:
2. sort functions and array_multisort(). But apparently it sorts several arrays at once, or a multi-dimensional array.

3. I also tried to define the following compare function:

function cmp($a, $b)
{
    foreach ($a as $key1 => $val1) {
        foreach ($b as $key2 => $val2) {
            if($val1 == $val2){
                return strcmp($key1,$key2);
            }
            else if ($val1 > $val2){
                return 1;
            }
            else{ // $val1 < $val2
                return -1;
            }
        }
    }    
} 

and call it with usort() but it also didn’t work.

So my question is: is there a PHP method that implements the requested behavior?

For Eugen:
I tried it and it doesn’t work
before sorting:

Array
(
    [lamb] => 3
    [rule] => 1
    [children] => 1
    [teacher] => 2
    [eager] => 1
)

and after sorting:

Array
(
    [children] => 1
    [eager] => 1
    [rule] => 1
    [teacher] => 2
    [lamb] => 3
)
  • 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-05T23:32:31+00:00Added an answer on June 5, 2026 at 11:32 pm

    You can use array_multisort

    <?php
        $arr = Array
        (        
            'banana' => 2,
            'cherry' => 1,
            'orange' => 3,
            'grapefruit' => 1,
            'apple' => 1
        );
    
        $values = array_values($arr);
        $keys = array_keys($arr);
    
        //first sort by values desc, then sort by keys asc
        array_multisort($values, SORT_DESC, $keys, SORT_ASC, $arr);
    
        print_r($arr);
        // output:
        /*
        Array
        (
            [orange] => 3
            [banana] => 2
            [apple] => 1
            [cherry] => 1
            [grapefruit] => 1
        )
        */
    
    ?>
    

    It works like this:

    • for each column used to sort (values and keys for you), create new 1d array with its contents
    • pass those 1d arrays to array_multisort function in your sorting order (so $values first, then $keys), add sort order for each array
    • the last argument has to be the array which you want to sort

    (Perhaps you will find this explanation easier to understand)

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

Sidebar

Related Questions

Consider this contrived example. The following array works fine if I want to convert
Consider following string Some string with quotes and \pre-slashed\ quotes Using regex, I want
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider following code: char str[] = Hello\0; What is the length of str array,
Consider following array $details = array( array('lname'=>'A', 'fname'=>'P','membkey'=>700,'head'=>'y'), array('lname'=>'B', 'fname'=>'Q','membkey'=>540,'head'=>'n'), array('lname'=>'C', 'fname'=>'R','membkey'=>700,'head'=>'n'), array('lname'=>'D', 'fname'=>'S','membkey'=>540,'head'=>'y'),
Let's consider following class template of custom array in Microsoft Visual C++ (Microsoft Visual
Consider the three following classes: EntityTransformer contains a map associating an Entity with a
Consider following make: all: a b a: echo a exit 1 b: echo b
Consider following code: My problem is: 1) I can't seem to cast the errors
Consider following text: $content=<<<EOT { translatorID: f4a5876a-3e53-40e2-9032-d99a30d7a6fc, label: ACL, creator: Nathan Schneider, target: ^https?://(www[.])?aclweb\\.org/anthology-new/[^#]+,

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.