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"]=&g开发者_开发知识库t;
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!
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);
Use usort()
then create two comparison functions and sort your array using them one after one.
精彩评论