Jump to content

[SOLVED] Sorting an array in this way?


m1key

Recommended Posts

Hi,

 

I've got an array which currently looks like this:

 

array(3) {
  [0]=>
  array(5) {
    [0]=>
    int(1)
    [1]=>
    int(2)
    [2]=>
    int(3)
    [3]=>
    int(5)
    [4]=>
    int(9)
  }
  [1]=>
  array(5) {
    [0]=>
    int(2)
    [1]=>
    int(0)
    [2]=>
    int(6)
    [3]=>
    int(4)
    [4]=>
    int(16)
  }
  [2]=>
  array(5) {
    [0]=>
    int(3)
    [1]=>
    int(1)
    [2]=>
    int(5)
    [3]=>
    int(4)
    [4]=>
    int(11)
  }
}

 

How can I sort it so that it sorts on index value '4' descending - i.e, I want the array to look like this:

 

array(3) {
  [0]=>
  array(5) {
    [0]=>
    int(2)
    [1]=>
    int(0)
    [2]=>
    int(6)
    [3]=>
    int(4)
    [4]=>
    int(16) <--------- Highest value
  }
  [1]=>
  array(5) {
    [0]=>
    int(3)
    [1]=>
    int(1)
    [2]=>
    int(5)
    [3]=>
    int(4)
    [4]=>
    int(11)
  }
  [2]=>
  array(5) {
    [0]=>
    int(1)
    [1]=>
    int(2)
    [2]=>
    int(3)
    [3]=>
    int(5)
    [4]=>
    int(9) <--------- Lowest value
  }
}

 

Cheers,

 

Mike.

Link to comment
Share on other sites

Hi,

 

Many thanks for the response. I'm not sure if usort() will be entirely appropriate, because this array is essentially a collection of user-collected data, and could possibly contain 50+ sets. I only included 3 sets in my example above for comparison purposes.

 

Apologies for not stating this in my original post :-[

Link to comment
Share on other sites

Ah, I think I was after usort() after all! This PHP lark is confusing sometimes  ;D

 

This did what I was after:

 

function highest($b, $a)

{ return $a[4] > $b[4];

}

usort($finalResults,'highest');

 

Thanks for the help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.