Page 1 of 1

Sorting arrays

Posted: Wed Apr 16, 2008 3:10 am
by aceconcepts
hi,

I need to sort my array by values in a nested array.

Here is the structure of my array: Array ( [0] => Array ( [1] => Array ( [0] => Array ( [1] => 12 ) ) ) )

The value I want to sort by is the last value in the last array ([1]=>12)

How would I go about doing this? I've been looking at usort() but cant seem to get it to work.

Re: Sorting arrays

Posted: Wed Apr 16, 2008 10:23 am
by pickle
array_multisort() is perfect for this sort of thing.

Re: Sorting arrays

Posted: Thu Apr 17, 2008 3:42 am
by aceconcepts
I am trying to sort my array by the last values in the last nested array. Using array_multisort() would you mind showing an example of how I might implement this sorting function for such an array?

I have tried many different ways of doing it and I am just getting nowhere.

Thanks for your help.

Re: Sorting arrays

Posted: Thu Apr 17, 2008 4:29 am
by Kieran Huggins
http://php.net/usort FTW!

Code: Select all

// the callback comparison function
function acecompare($a,$b){
    if($a[1][0][1] == $b[1][0][1]) return 0;
    return ($a[1][0][1] < $b[1][0][1]) ? -1 : 1;
}
 
// the usort call
usort($your_massive_multidimensional_array, "acecompare");