Search code examples
phparrayssortingmultidimensional-array

Sort an associative array by key in a descending direction


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.

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

Here's a real excerpt from the array: http://pastebin.com/DTngBiH5


Solution

  • 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']);