Sort Array by Element

Go To StackoverFlow.com

1

I have a very large multidimensional array and I would like to sort it decending by the value of an element. Here's an example of the array:

[match_info]  
    [123]  
        [match_id] => [123]  
    [124]  
        [match_id[ => [124]  

So really, I guess I'd like to sort the data of the initial "name" of the next layer of the array- so the 123 and 124, not the [match_id] (even though they are the same value, I feel like it would be best to just sort from the first value listed). I've been looking at usort but don't fully comprehend the compare functions. Any help would be awesome!

Also, the result I'm looking for is [124] to be above [123] :P

Here's a real excerpt from the array: http://pastebin.com/DTngBiH5 (I hate to link to you an external site but... too large)

2012-04-04 02:35
by Kevin Murphy
I'm not understanding your question. Can you perhaps post an excerpt of print_r() output, so we can see exactly how the array is structured? Also, what have you tried so far, what did it do, and what were you expecting? Example data and example code will get you the best/fastest results - ghoti 2012-04-04 02:37
http://pastebin.com/DTngBiH5 here's an excerpt - I cut out some useless information to make it easier on the eyes. Basically, the numbers I need to sort everything by are 85174405, 85176557, etc. Underneath those are the IDs of people inside. Those don't matter. I just need to basically rearrange the order of the entire array to have the larger Match IDs at the top. (The Match IDs are the first elements of [match_summ] and are used further down in the array.

I tried asort(); but it remained unchanged since the command wasn't sorting by something (I assume) - Kevin Murphy 2012-04-04 03:09



2

Have a look at krsort function. It sorts an array by key in reverse order (or just ksort if reverse is not required).

krsort($array['match_info']);
2012-04-04 02:49
by Alexei Tenitski
What if I wanted to sort contents deeper inside the array - Kevin Murphy 2012-04-04 11:05
You can use array_multisort for more advanced sorting. Here is a tutorial about various PHP sort functions http://www.techrepublic.com/blog/programming-and-development/use-the-power-of-these-php-functions-to-sort-your-arrays/39 - Alexei Tenitski 2012-04-04 11:15
I guess the better question is - can I just do multiple krsorts and just generate a totally new array with the entire thing reordered - Kevin Murphy 2012-04-04 23:39
First of all note that ksort modifies the array you passed to it, it does not return a new array. Not sure if this is answering your question however there's nothing stopping you from doing ksort on any level of the array eg ksort($arr['one']['two']['three'] - Alexei Tenitski 2012-04-04 23:52
Is it possible to do multiple sorts at once like: ksort($arr['matchid], $arr['teamsumm'] and it sorts them both? Or could I do
ksort($arr['matchid];
ksort($arr['team
summ];
I just want to sort different elements of the array if that makes any sense - Kevin Murphy 2012-04-05 06:27
You would have to sort them separetl - Alexei Tenitski 2012-04-05 19:32