0

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 ) 

 )
user875293
  • 619
  • 4
  • 12
  • 20

2 Answers2

1

Try this:

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

usort($_SESSION['details'], "cmp");
morawcik
  • 92
  • 6
0

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");
Joshua Kissoon
  • 3,155
  • 5
  • 26
  • 57