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
https://forums.phpfreaks.com/topic/100653-solved-sorting-an-array-in-this-way/
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 :-[

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.