views:

64

answers:

2

Hello.
I have a 'simple' multidimensional array which looks like this:

array(3) {
  ["user 1"]=>
  array(4) {
    [0]=>
    array(1) {
      ["item 4"]=>
      string(5) "11385"
    }
    [1]=>
    array(1) {
      ["item 2"]=>
      string(6) "144268"
    }
    [2]=>
    array(1) {
      ["item 1"]=>
      string(5) "65774"
    }
    [3]=>
    array(1) {
      ["item 9"]=>
      string(5) "98523"
    }
  }
  ["user 5"]=>
  array(1) {
    [0]=>
    array(1) {
      ["item 8"]=>
      string(6) "239233"
    }
  }
  ["user 2"]=>
  array(2) {
    [0]=>
    array(1) {
      ["item 4"]=>
      string(5) "53718"
    }
    [1]=>
    array(1) {
      ["item 1"]=>
      string(6) "154687"
    }
  }
}

What I need to do is to simply sort my array first by users, then by items. Ascending. How could I do this? I will provide some code if necessary :)

Thanks!

+1  A: 

Use usort (see php.net for more information) then create two comparison functions and sort your array using them one after one.

Thariama
+5  A: 

ksort sorts by the key

$newArray = array();

# start by sorting users
$yourArray = ksort($yourArray);

# then sort sub items
foreach($yourArray as $user=>$theirItems) {
    $theirItems = ksort($theirItems); # assuming you still want to sort by key;
    $newArray[$user] = $theirItems;
}

var_dump($newArray);
Lizard