Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

Hello everybody :) this is my first question, so I hope for answer :) I've got an array of arrays like this :

Array(
    [] => Array ([category] => 3 )
    [] => Array ([price] => 5 )
    [] => Array ([rating] => 1 )
    [] => Array ([price] => 3 )
    [] => Array ([category] => 1 )
    [] => Array ([category] => 2 )
    )
)

Question : How could I sort it for ex. alphanumerically by subarray key and subarray value, so that it is converted like the one following?

Array(
    [] => Array ([category] => 1 )
    [] => Array ([category] => 2 )
    [] => Array ([category] => 3 )
    [] => Array ([price] => 3 )
    [] => Array ([price] => 5 )
    [] => Array ([rating] => 1 )
)
share|improve this question
    
possible duplicate of Sort Multi-dimensional Array by Value – dynamic Nov 9 '14 at 16:44
    
you can use usort() – Avinash Babu Nov 9 '14 at 16:44

1 Answer 1

up vote 2 down vote accepted

You're looking for array_multisort:

$x = Array(
    Array ("category" => 3 ),
    Array ("price" => 5 ),
    Array ("rating" => 1 ),
    Array ("price" => 3 ),
    Array ("category" => 1 ),
    Array ("category" => 2 ),

);

array_multisort(
    array_map('key', $x), 
    array_map('current', $x), 
    $x);

print_r($x);
share|improve this answer
    
Great! Works perfect! Thank you – Radim Kleinpeter Nov 9 '14 at 16:57
    
@RadimKleinpeter: not at all, and welcome to Stack Overflow! – georg Nov 9 '14 at 17:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.