PHP sort multidimensional array by several criteria

Discussion in 'PHP' started by xms, Mar 27, 2017.

  1. #1
    I am coding with PHP and having a multidimensional array. I would like to sort my array. The most important criteria is total points. The second criteria is name.

    I do not want to destroy indexes. I have tried usort, uasort, uksort, etc., but this just does not work.

    As said, the array should be sorted that way:

    1st by [info][total], descending
    2nd by [info][name], ascending

        Array (
          [183] => Array (
            [3] => Array (
              [1] => Array ()
            )
       
            [1] => Array (
              [8] => Array ()
            )
       
            [info] => Array (
              [name] => Doe John /* 2nd criteria */
              [total] => 4800 /* 1st criteria */
            )
          )
       
          [380] => Array (
            [4] => Array (
              [6] => Array ()
            )
       
            [info] => Array (
              [name] => Davis Ben /* 2nd criteria */
              [total] => 14500 /* 1st criteria */
            )
       
            [2] => Array (
              [5] => Array ()
            )
          )
        )
    Code (markup):
    So, the result should be like this:

        Array (
          [380] => Array (
            [4] => Array (
              [6] => Array ()
            )
       
            [info] => Array (
              [name] => Davis Ben
              [total] => 14500
            )
       
            [2] => Array (
              [5] => Array ()
            )
          )
       
          [183] => Array (
            [3] => Array (
              [1] => Array ()
            )
       
            [1] => Array (
              [8] => Array ()
            )
       
            [info] => Array (
              [name] => Doe John
              [total] => 4800
            )
          )
        )
    Code (markup):
    I have tried this:

        foreach ($array as &$item)
        {
          uasort($item, function ($a, $b)
          {
            if ($a['info']['total'] == $b['info']['total'])
            {
              return $b['info']['name'] - $a['info']['name'];
            }
            else
            {
              return $b['info']['total'] - $a['info']['total'];
            }
          });
        }
    PHP:
     
    xms, Mar 27, 2017 IP
  2. xms

    xms Active Member

    Messages:
    169
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Here it is:

        uasort($array, function($a, $b)
        {
          if ($a['info']['total'] == $b['info']['total'])
          {
            return strcmp($a['info']['name'], $b['info']['name']); # ascending
          }
    
          return $b['info']['total'] - $a['info']['total']; # descending
        });
    PHP:
     
    xms, Mar 27, 2017 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,875
    Likes Received:
    4,547
    Best Answers:
    123
    Trophy Points:
    665
    #3
    so, that second post is the solution?
     
    sarahk, Mar 27, 2017 IP
  4. xms

    xms Active Member

    Messages:
    169
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #4
    Yes, it seems to do the job.
     
    xms, Mar 27, 2017 IP