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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:37:32+00:00 2026-06-01T10:37:32+00:00

I tried to search and found this: Sort an array by a child array's

  • 0

I tried to search and found this:

Sort an array by a child array's value in PHP

But the function does not work in my case:

                $sorted = array();
                foreach($players as $player)
                {
                    $p = Model::factory('user');
                    $p->load($player['id']);

                    $sorted[] = array('id' => $player['id'], 'username' => $p->get_username());
                }

How can i sort the array alphabetic after the username?

The function,

function cmp($a, $b) {
        if ($a['username'] == $b['username']) {
                return 0;
        }
        return ($a['username'] < $b['username']) ? -1 : 1;
}

and then calling usort($sorted,”cmp”); will not work out for me (getting error undefined index[2])..

And is there any way to choose whether it should be sorting descending or ascending?

  • 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-01T10:37:33+00:00Added an answer on June 1, 2026 at 10:37 am

    The ‘cmp’ function will be:

    // $param - the parameter by which you want to search
    function cmp(&$a, &$b, $param) {
        switch( $param ) {
            case 'id':
                if ( $a['id'] == $b['id'] ) {
                    return 0;
                }
    
                return ( $a['id'] < $b['id'] ) ? -1 : 1;
                break;
            case 'username':
                // string comparison
                return strcmp($a['username'], $b['username']);
                break;
        }
    }
    
    // this is the sorting function by using an anonymous function
    // it is needed to pass the sorting criterion (sort by id / username )
    usort( $sorted, function( $a,$b ) {
        return cmp( $a, $b, 'username');
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to search online to solve this question but I didn't found anything.
tried to search for this, although I am not even sure what I am
I've tried to search for a solution for this, but have had no joy:
I've tried searching through search engines,MSDN,etc. but can't anything. Sorry if this has been
I've tried to search online, but haven't found a clear answer for my issue
I know this might be a common question but I have tried to search
Yes, I've tried to search answer for this problem and I found many similar
I tried to search this site and all over the internet, but I haven't
I tried to search for a JavaScript reference, but there's none available. The best
Hopefully this isn't a duplicate. I tried to search for an answer to my

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.