按嵌套元素排序php数组排序[复制]

This question already has an answer here:

I have a php session $_SESSION[‘details’] and when i print_r returns the following.

Array (
[0] => Array ( [nameID] => 1357325402 [vol] => 1 [catID] => 2 ) 
[1] => Array ( [nameID] => 1357325423 [vol] => 1 [catID] => 2 ) 
[2] => Array ( [nameID] => 1357325470 [vol] => 1 [catID] => 10 ) 
[3] => Array ( [nameID] => 1357325440 [vol] => 1 [catID] => 4 ) 
[4] => Array ( [nameID] => 1357325416 [vol] => 1 [catID] => 2 )
[5] => Array ( [nameID] => 1357325471 [vol] => 1 [catID] => 10 ) 
 )

How can a sort the array so all the the catID are together?

Array (
[0] => Array ( [nameID] => 1357325402 [vol] => 1 [catID] => 2 ) 
[1] => Array ( [nameID] => 1357325423 [vol] => 1 [catID] => 2 ) 
[2] => Array ( [nameID] => 1357325416 [vol] => 1 [catID] => 2 )
[3] => Array ( [nameID] => 1357325440 [vol] => 1 [catID] => 4 ) 
[4] => Array ( [nameID] => 1357325470 [vol] => 1 [catID] => 10 ) 
[5] => Array ( [nameID] => 1357325471 [vol] => 1 [catID] => 10 ) 

 )
</div>

Try this:

function cmp($a, $b){
    return strcmp($a["catID"], $b["catID"]);
}

usort($_SESSION['details'], "cmp");

You can use PHP's usort function and supply your own comparison function. Like this:

function cmp($a, $b)
{
    return ($a["catID"] > $b["catID"]) ? 1 : -1;  // Ascending order
}

usort($_SESSION[‘details’], "cmp");