0

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 )
)
1

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.