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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:35:40+00:00 2026-05-13T05:35:40+00:00

I have some difficulty sorting my array. It looks like this : [0] =>

  • 0

I have some difficulty sorting my array. It looks like this :

[0] => Array
        (
            [firstname] => Jnic
            [lastname] => Fortin
            [points] => Array
                (
                    [id] => 20453
                    [f] => 31
                    [r] => 7                 
                    [total] => 82
                )

        )

    [1] => Array
        (
            [firstname] => Kris
            [lastname] => Anders
            [points] => Array
                (
                    [id] => 20309                    
                    [f] => 0
                    [r] => 1
                    [total] => 56
                )

        )
[2] => Array
            (
                [firstname] => Em
                [lastname] => Zajo
                [points] => Array
                    (
                        [id] => 20339                    
                        [f] => 8
                        [r] => 3
                        [total] => 254
                    )

            )

I would like to sort it by “total” DESC. How could I do it? If everything sort ok the array would be order [2][0][1] (254,82,56)

  • 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-13T05:35:40+00:00Added an answer on May 13, 2026 at 5:35 am

    You probably can use the usort function for that : it sorts an array, using a callback function to compare the elements of that array :

    bool usort  ( array &$array  , callback $cmp_function  )
    

    This function will sort an array by
    its values using a user-supplied
    comparison function. If the array you
    wish to sort needs to be sorted by
    some non-trivial criteria, you should
    use this function

    If your function is defined to compare per $element['points']['total'], it should do the trick.

    Edit : And here is the example, using uasort, which is the same as usort, but will keep the array keys, like pointed out by ryanday :

    First, let’s declare the array :

    $a = array(
        array(
            'firstname' => 'Jnic',
            'lastname' => 'Fortin',
            'points' => array(
                'id' => 20453,
                'f' => 31,
                'r' => 7,
                'total' => 82,
            ),
        ),
        array(
            'firstname' => 'Kris',
            'lastname' => 'Anders',
            'points' => array(
                'id' => 20309,
                'f' => 0,
                'r' => 1,
                'total' => 56,
            ),
        ),
        array(
            'firstname' => 'Em',
            'lastname' => 'Zajo',
            'points' => array(
                'id' => 20339,
                'f' => 8,
                'r' => 3,
                'total' => 254,
            ),
        ),
    );
    

    And then, the comparison function :

    function my_compare($a, $b) {
        if ($a['points']['total'] > $b['points']['total']) {
            return -1;
        } else if ($a['points']['total'] < $b['points']['total']) {
            return 1;
        }
        return 0;
    }
    

    And, finally, we use it :

    uasort($a, 'my_compare');
    var_dump($a);
    

    And get the array, sorted by total desc :

    array
      2 => 
        array
          'firstname' => string 'Em' (length=2)
          'lastname' => string 'Zajo' (length=4)
          'points' => 
            array
              'id' => int 20339
              'f' => int 8
              'r' => int 3
              'total' => int 254
      0 => 
        array
          'firstname' => string 'Jnic' (length=4)
          'lastname' => string 'Fortin' (length=6)
          'points' => 
            array
              'id' => int 20453
              'f' => int 31
              'r' => int 7
              'total' => int 82
      1 => 
        array
          'firstname' => string 'Kris' (length=4)
          'lastname' => string 'Anders' (length=6)
          'points' => 
            array
              'id' => int 20309
              'f' => int 0
              'r' => int 1
              'total' => int 56
    

    ryanday > Thanks for your answer !

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

Sidebar

Related Questions

I have some XML which contains records and sub records, like this: <data> <record
I have some jQuery code that is having difficulty running because I'm using it
i have a little difficulty in understanding how to do some INSERT SELECT. For
I have had a bit of a look around and am having some difficulty
I'm having some difficulty getting my cross domain policy to work. I have an
I have implemented drag and drop in my application, but am having some difficulty
I'm having some difficulty with this, but basically load a page into a QWebView
I'm new to core data and having some difficulty finding any information about sorting
I'm not exactly a regex guru, so I have some difficulty finding a regular
I have had quite some difficulty trying to make a User Defined Function (UDF)

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.